C# 使用实体框架运行IQueryable

C# 使用实体框架运行IQueryable,c#,linq,entity-framework,C#,Linq,Entity Framework,我正在用C#和.NET Framework 4.0开发实体框架代码First 4.4.0.0库 我正在搜索如何使用where子句上的数组动态创建查询。我发现了这个(): IQueryable SearchProducts(参数字符串[]关键字) { IQueryable query=dataContext.Products; foreach(关键字中的字符串关键字) { 字符串temp=关键字; query=query.Where(p=>p.Description.Contains(temp))

我正在用C#和.NET Framework 4.0开发实体框架代码First 4.4.0.0库

我正在搜索如何使用where子句上的数组动态创建查询。我发现了这个():

IQueryable SearchProducts(参数字符串[]关键字)
{
IQueryable query=dataContext.Products;
foreach(关键字中的字符串关键字)
{
字符串temp=关键字;
query=query.Where(p=>p.Description.Contains(temp));
}
返回查询;
}
但我不知道如何使用它如何运行此查询?

我通常这样做:

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;
    }
}
使用(var context=new MyContext())
{
context.Configuration.ProxyCreationEnabled=false;
var users=来自上下文中的u.users.Include(“WhoHasBlockedMe”).Include(“朋友”)
来自美国的act
其中act.ActivityId==ActivityId&&
u、 性别==性别ID&&
u、 城市&&
u、 Country==countryIsoCode
选择u;
//此用户未退出数据库
if((用户==null)||
(users.Count()=0))
{
ctx.StatusCode=System.Net.HttpStatusCode.NotFound;
ctx.suppresentitybody=true;
}
其他的
{
usersList=新列表();
foreach(users.ToList()中的用户us)
{
添加(UserProfile.CreateUserView(us,userId));
}
ctx.StatusCode=System.Net.HttpStatusCode.OK;
}
}

如何运行该查询?

您应该执行查询并获得结果:

query.ToList()
在您的示例中,它看起来如下所示:

var users = SearchProducts().ToList();
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)
    {
        usersList.Add(UserProfile.CreateUserView(us, userId));
    }
    ctx.StatusCode = System.Net.HttpStatusCode.OK;
}
var users=SearchProducts().ToList();
if((users==null)| |(users.Count()==0))
{
ctx.StatusCode=System.Net.HttpStatusCode.NotFound;
ctx.suppresentitybody=true;
}
其他的
{
usersList=新列表();
foreach(用户中的用户us)
{
添加(UserProfile.CreateUserView(us,userId));
}
ctx.StatusCode=System.Net.HttpStatusCode.OK;
}

非常感谢您的回答。
var users = SearchProducts().ToList();
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)
    {
        usersList.Add(UserProfile.CreateUserView(us, userId));
    }
    ctx.StatusCode = System.Net.HttpStatusCode.OK;
}