LINQ中的动态WHERE子句将动态WHERE子句组装到LINQ语句的最佳方法是什么?我在表单上有几十个复选框,并将它们传递回:Dictionary <string,List <string >>(Dictionary <fieldName,List <values >>)到我的LINQ查询。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;}
3 回答
ibeautiful
TA贡献1993条经验 获得超5个赞
我有类似的场景,我需要根据用户输入添加过滤器,并链接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);
- 3 回答
- 0 关注
- 959 浏览
添加回答
举报
0/150
提交
取消