我正在使用 C# 和 .NET Framework 4.0 开发 Entity Framework Code First 4.4.0.0 库。
我正在搜索如何使用 where 子句上的数组动态创建查询。我发现了这个(在这里):
IQueryable<Product> SearchProducts (params string[] keywords)
{
IQueryable<Product> query = dataContext.Products;
foreach (string keyword in keywords)
{
string temp = keyword;
query = query.Where (p => p.Description.Contains (temp));
}
return query;
}
但我不知道如何使用它。如何运行此查询?
我通常这样做:
using (var context = new MyContext())
{
context.Configuration.ProxyCreationEnabled = false;
var users = from u in context.Users.Include("WhoHasBlockedMe").Include("Friends")
from act in u.WantsToDo
where act.ActivityId == activityId &&
u.Gender == genderId &&
u.City == city &&
u.Country == countryIsoCode
select u;
// This user doesn't exit on database
if ((users == null) ||
(users.Count() == 0))
{
ctx.StatusCode = System.Net.HttpStatusCode.NotFound;
ctx.SuppressEntityBody = true;
}
else
{
usersList = new List<UserProfile>();
foreach(User us in users.ToList())
{
usersList.Add(UserProfile.CreateUserView(us, userId));
}
ctx.StatusCode = System.Net.HttpStatusCode.OK;
}
}
如何运行该查询?