var includeState = string.IsNullOrEmpty(state);
var lstSkillIds = new List<int>(){1,28};
var includeSkill = lstSkillIds.Count <= 0;
//Query to search all directory users for matching city and last name
var advisors = (from directoryObj in PersonDirectories
from user in directoryObj.Users
from address in user.Address
let skills = user.UserSkills
where (directoryId == 0 || directoryObj.PersonDirectoryId == directoryId)
&& user != null
//Apply where clause for State filter if state is provided
&& ((address != null && !string.IsNullOrEmpty(address.CountryCode)
&& (address.CountryCode.ToLower() == country.ToLower()
&& (includeState || (!string.IsNullOrEmpty(address.Province)
&& address.Province.ToLower() == state.ToLower())))))
// add filter to get users with skills given in lstSkillIds
&& ( includeSkill || !lstSkillIds.Except(user.UserSkills.Select(u => u.UserSkillId)).Any())
//Get unique search results
group user by new
{
user.PublicId,
address
}
into userGroup
select new DirectoryUserObject
{
PublicId = userGroup.Key.PublicId,
Profile = userGroup.FirstOrDefault().Profile,
Address = userGroup.Key.address,
DirSubscription = (int) userGroup.FirstOrDefault().Profile.DirSubscription
});