I don't have a lot of experience with IQueryable
What I am trying to do is search for a user based on a list of passed in constraints that can either be a username, or phone number. Depending on the type I want to return limited information. I then want to combine the 3 IQueryables into one and combine entries with matching id and username to maintain the most information.
Here is what i have so far:
public IQueryable<User> Search(String[] criteria)
{
var query = Database.Get<User>();
IQueryable<User> phoneQuery = null;
IQueryable<User> emailQuery = null;
IQueryable<User> nameQuery = null;
foreach (String str in criteria)
{
// Check if it is a phone number
if (Regex.IsMatch(str, @"([0-9]{3})?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$"))
{
phoneQuery = query.Where(
u => u.PhoneNumber.ToLower() == (str))
.Select(i => new User
{
Id = i.Id,
UserName = i.Name,
PhoneNumber = i.PhoneNumber
})
}
// check if it is an email
else if (criteria.Contains("@"))
{
emailQuery = query.Where(
u => u.Email.ToLower() == (str))
.Select(i => new User
{
Id = i.Id,
UserName = i.Name,
Email = i.Email
})
}
else
{
nameQuery = query.Where(
u => u.UserName.ToLower() == (str))
.Select(i => new User
{
Id = i.Id,
UserName = i.Name,
})
}
}
// Merge the 3 queries combining entries if the username and id match and maintain the maximum amount of information
return query;
}