Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ中的动态where子句?_C#_Linq_Linq To Sql - Fatal编程技术网

C# LINQ中的动态where子句?

C# LINQ中的动态where子句?,c#,linq,linq-to-sql,C#,Linq,Linq To Sql,我正在尝试根据动态where条件加载数据 string tempQry = string.Empty; if (!string.IsNullOrEmpty(cusid) && !string.IsNullOrEmpty(mktid)) tempQry = "x=>x.MarketID==" + mktid + "&& x.MasterCustomerID==" + cusid; if (string.IsNullOrEmpty(cusid))

我正在尝试根据动态where条件加载数据

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());
string tempQry=string.Empty;
如果(!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=新建链接列表(
(从一个in lstuOptininterest
加入b_marketoEntities.custcompreferences.Where(tempQry)
在新的{CODE=a.CODE,SUBCODE=a.SUBCODE}上等于新的{CODE=b.Option\u Short\u Name,SUBCODE=b.Option\u Short\u Subname}
左组
来自leftGroup.DefaultIfEmpty()中的b
选择新选项TereStareA()
{
代码=a.代码,
子代码=a.子代码,
SubCodeDescription=a.SubCodeDescription,
CodeDescription=a.CodeDescription,
PrevOptIn=b!=null&&b.OptedIn==true
}).ToList());
它给出了编译错误
Where(tempQry)


“System.Data.Entity.DbSet”不包含“Where”的定义和最佳扩展方法重载“System.Linq.Queryable.Where(System.Linq.IQueryable,System.Linq.Expressions.Expression)”具有一些无效参数


如何处理

其中
以lambda而不是字符串的形式等待条件,因此您必须稍微重构代码(如下所示):


请按查看此博客。它将帮助您对问题进行排序

您看到的错误似乎表明您正在使用EF not LINQ to SQL。如果是这样,请更正您的标签。如果要使用字符串,请考虑使用方法而不是使用dBSET。或者,您可以使用构建整个查询。

请正确设置代码格式。现在很难阅读。请查看此博客以获取您的答案。您看到的错误似乎表明您正在使用EF而不是LINQ to SQL。如果是这样,请更正您的标签。如果您想使用字符串,请考虑使用方法。@ Anderi说<代码>不能隐式地将类型“Stase.LINQ.IQueReable”转换为“Stase.DATA .NET.DATSET”。存在显式转换(是否缺少转换?@James123,请尝试显式指定类型。有关示例,请参阅更新的答案。
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
...