将动态WHERE子句组装到LINQ语句的最佳方法是什么?
我在表单上有几十个复选框,并将它们作为:Dictionary
public IOrderedQueryable<ProductDetail> GetProductList(string productGroupName, string productTypeName, Dictionary<string,List<string>> filterDictionary)
{
var q = from c in db.ProductDetail
where c.ProductGroupName == productGroupName && c.ProductTypeName == productTypeName
// insert dynamic filter here
orderby c.ProductTypeName
select c;
return q;
}
如果有人感兴趣,这是我想出的解决方案。
只需分享我对此案的想法。
我有类似的情况,我需要根据用户输入添加过滤器,并链接where子句。
这里是示例代码。
var votes = db.Votes.Where(r => r.SurveyID == surveyId);
if (fromDate != null)
{
votes = votes.Where(r => r.VoteDate.Value >= fromDate);
}
if (toDate != null)
{
votes = votes.Where(r => r.VoteDate.Value <= toDate);
}
votes = votes.Take(LimitRows).OrderByDescending(r => r.VoteDate);
您还可以使用LinqKit中的PredicateBuilder使用Or或And链接多个类型安全的lambda表达式。
一种简单的方法可以是,如果您的列是简单类型的,例如字符串
public static IEnumerable<MyObject> WhereQuery(IEnumerable<MyObject> source, string columnName, string propertyValue)
{
return source.Where(m => { return m.GetType().GetProperty(columnName).GetValue(m, null).ToString().StartsWith(propertyValue); });
}
我想出了一个即使我也能理解的解决方案...通过使用“包含”方法,您可以根据需要链接多个WHERE。如果WHERE是空字符串,则将其忽略(或评估为全选)。这是我在LINQ中联接2个表,应用多个where子句并填充要返回到视图的模型类的示例。 (这是一个全选)。
public ActionResult Index()
{
string AssetGroupCode = "";
string StatusCode = "";
string SearchString = "";
var mdl = from a in _db.Assets
join t in _db.Tags on a.ASSETID equals t.ASSETID
where a.ASSETGROUPCODE.Contains(AssetGroupCode)
&& a.STATUSCODE.Contains(StatusCode)
&& (
a.PO.Contains(SearchString)
|| a.MODEL.Contains(SearchString)
|| a.USERNAME.Contains(SearchString)
|| a.LOCATION.Contains(SearchString)
|| t.TAGNUMBER.Contains(SearchString)
|| t.SERIALNUMBER.Contains(SearchString)
)
select new AssetListView
{
AssetId = a.ASSETID,
TagId = t.TAGID,
PO = a.PO,
Model = a.MODEL,
UserName = a.USERNAME,
Location = a.LOCATION,
Tag = t.TAGNUMBER,
SerialNum = t.SERIALNUMBER
};
return View(mdl);
}
使用三元运算符动态决定是否包含条件似乎越来越简单
列表productList = new List();
productList =
db.ProductDetail.Where(p => p.ProductDetailID > 0 //Example prop
&& (String.IsNullOrEmpty(iproductGroupName) ? (true):(p.iproductGroupName.Equals(iproductGroupName)) ) //use ternary operator to make the condition dynamic
&& (ID == 0 ? (true) : (p.ID == IDParam))
).ToList();
我有相同的问题(User defined filter for linq,@tvanfosson告诉了我有关Dynamic Linq(http://code.msdn.microsoft.com/csharpsamples)。
您可以使用Any()扩展方法。以下内容似乎对我有用。
XStreamingElement root = new XStreamingElement("Results",
from el in StreamProductItem(file)
where fieldsToSearch.Any(s => el.Element(s) != null && el.Element(s).Value.Contains(searchTerm))
select fieldsToReturn.Select(r => (r == "product") ? el : el.Element(r))
);
Console.WriteLine(root.ToString());
其中“ fieldsToSearch”和“ fieldsToReturn”都是列表对象。
CodePlex上的这个项目有您想要的。
System.Linq.Dynamic-http://dynamiclinq.codeplex.com/
项目描述
扩展System.Linq.Dynamic以支持针对实体框架或任何支持IQueryable的提供程序执行在字符串中定义的Lambda表达式。
由于它是源代码的扩展,您可以在Scott Guthrie's Blog上找到它,它使您可以执行以下操作:
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS82cEY3cy5wbmcifQ==” alt =“在此处输入图像说明”>“ >>
以及类似的内容:
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9mQ2JXay5wbmcifQ==” alt =“在此处输入图像描述”>