C# 具有多个where参数的linq到sql查询

C# 具有多个where参数的linq到sql查询,c#,sql,asp.net-mvc,linq,entity-framework,C#,Sql,Asp.net Mvc,Linq,Entity Framework,我目前正在用实体框架在ASP.NETMVC4中编写一个搜索函数。然而,我遇到了一个障碍,我只能找到“糟糕”的解决方案 我的搜索函数返回一个由4个参数组成的模型: String Name String Street String Code String Province List<Query> query = (from t in Model select t).ToList(); 字符串名 弦街 字符串代码 弦省 列表查询=(从模型中的t选择t).ToList(); 现在我想过

我目前正在用实体框架在ASP.NETMVC4中编写一个搜索函数。然而,我遇到了一个障碍,我只能找到“糟糕”的解决方案

我的搜索函数返回一个由4个参数组成的模型:

String Name
String Street
String Code
String Province

List<Query> query = (from t in Model select t).ToList();
字符串名
弦街
字符串代码
弦省
列表查询=(从模型中的t选择t).ToList();
现在我想过滤我的搜索输入。但是,用户可以决定填写尽可能多的搜索字段。他可以决定使用名称和街道,或名称,街道和省,或

我能找到的唯一真正的解决方案是进行查询和
IQueryable
,并检查字段是否已填充
如果
,然后使用
。Where
更新查询。 由于这将提供M5查询,我想知道是否有一个更好的解决方案,我在这里错过了


谢谢你的帮助。

如果我理解你的意思是正确的。您可能需要以下内容:

string Name;
string Street;
string Code;
string Province;
var query=(from t in Model select t);
if(Name!=null)
{
    query=query.Where (q =>q.Name==Name);
}
if(Street!=null)
{
    query=query.Where (q =>q.Street==Street);
}
if(Code!=null)
{
    query=query.Where (q =>q.Code==Code);
}
if(Province!=null)
{
    query=query.Where (q =>q.Province==Province);
}
List<Query> ls = query.ToList();
因此,当我们附加Where子句而不使用
ToList()
时。没有执行查询


请在

中尝试查询,您可以尝试类似的操作

from cars in tblCars
where (cars.colorID == 1) && (cars.Wieght > 500) && (cars.Active == true)
select cars;

使用您在此处找到的实体筛选器类:

因此,首先指定过滤器,然后将其应用于查询。

例如:

var filter = EntityFilter
.Where(c => c.Name == came)
.Where(c => c.City == city);

var customers = FindCustomers(filter);

Customer[] FindCustomers(IEntityFilter filter)
{
var query = context.Customers;
query = filter.Filter(query);
return query.ToArray();
}

更多信息:

Arion的解决方案当然非常好,我尝试使用反射减少重复性,希望能有所帮助

        Type myType = myObject.GetType();
        IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());

        foreach (PropertyInfo prop in props)
        {
            object propValue = prop.GetValue(myObject, null);
            if (propValue != null)
            {
                query = query.Where(q => prop.GetValue(q, null) == propValue);
            }
        }
Type myType=myObject.GetType();
IList props=新列表(myType.GetProperties());
foreach(PropertyInfo props in props)
{
object propValue=prop.GetValue(myObject,null);
if(propValue!=null)
{
query=query.Where(q=>prop.GetValue(q,null)==propValue);
}
}
编辑:


我已经对其进行了编辑,以处理所有属性。当然,您仍然需要一些东西才能使用它,但一旦您了解如何使用它,您就可以将其作为所有代码的实用工具,而不是对每种类型进行硬编码

是的,这是我在网上找到的解决方案。我想知道是否有更干净的方法来编写此代码。如果您想保持
IQueryable
。那么这就是我认为的方法,可能有更多的代码,但这是最优化的(可能是最好的)一个。在Why???中检查结果SQL查询??您正在检查某些内容是否为空(不复杂)。如果值是某物,那么添加where语句(不复杂)。我对这个解决方案有疑问。如果从数据库中恢复了大量数据,该怎么办?据我所知(如果我错了,请原谅),这将从数据库中恢复整个数据集合,然后根据指定的条件过滤结果,对吗?谢谢回答,但这仅适用于您指定的名称值。因为我还有3个其他的值,我不知道这是怎么回事。
        Type myType = myObject.GetType();
        IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());

        foreach (PropertyInfo prop in props)
        {
            object propValue = prop.GetValue(myObject, null);
            if (propValue != null)
            {
                query = query.Where(q => prop.GetValue(q, null) == propValue);
            }
        }