在LINQ动态where子句? [英] Dynamic where clause in LINQ?
本文介绍了在LINQ动态where子句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图加载基于动态WHERE条件的数据。
I am trying to load data based on Dynamic where condition.
string tempQry = string.Empty;
if (!string.IsNullOrEmpty(cusid) && !string.IsNullOrEmpty(mktid))
tempQry = "x=>x.MarketID==" + mktid + "&& x.MasterCustomerID==" + cusid;
if (string.IsNullOrEmpty(cusid))
tempQry = "x=>x.MarketID==" + mktid;
if (string.IsNullOrEmpty(mktid))
tempQry = "x=>x.MasterCustomerID==" + cusid;
_lstOptInInterest = new LinkedList<OptInInterestArea>(
(from a in _lstOptInInterest
join b in _marketoEntities.CustCommPreferences.Where(tempQry)
on new { CODE = a.Code, SUBCODE = a.SubCode } equals new { CODE = b.Option_Short_Name, SUBCODE = b.Option_Short_Subname }
into leftGroup
from b in leftGroup.DefaultIfEmpty()
select new OptInInterestArea()
{
Code = a.Code,
SubCode = a.SubCode,
SubCodeDescription = a.SubCodeDescription,
CodeDescription = a.CodeDescription,
PrevOptIn = b != null && b.OptedIn == true
}).ToList());
这是给编译错误凡(tempQry)
。
'System.Data.Entity.DbSet< Market.Data.CustCommPreference>'不包含定义去哪儿和最好的推广方法重载'System.Linq.Queryable.Where< TSource>(System.Linq.IQueryable< TSource>中System.Linq.Expressions.Expression< System.Func< TSource,布尔>>)'有一些无效参数
如何处理呢?
推荐答案
其中,
等待条件lambda表达式而不是字符串的形式,让你必须重构你的代码一点点(以下只是一个想法):
Where
awaits conditions in form of lambdas rather than strings, so you have to refactor your code a little bit (just an idea below):
IQueryable<CustCommPreference> query = _marketoEntities.CustCommPreferences.AsQueryable();
if (!string.IsNullOrEmpty(cusid))
query = query.Where(x => x.MasterCustomerID == cusid);
if (!string.IsNullOrEmpty(mktid))
query = query.Where(x => x.MarketID == mktid);
和以后使用它:
...
join b in query
...
这篇关于在LINQ动态where子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文