1

我有一个 LINQ 查询,它对独立列表工作正常,但对 CRM 失败

    var lst = new List<bool?>();
lst.Add(null);
lst.Add(true);
lst.Add(false);
bool IsWet = false;
var newlst = from exch_HideVoiceSignature in lst where 
(((exch_HideVoiceSignature!=null && exch_HideVoiceSignature==false 
||    exch_HideVoiceSignature== null) )&& !IsWet) select exch_HideVoiceSignature;
newlst.Dump();

var question = from q in exch_questionSet where ((q.exch_HideVoiceSignature != null 
&& q.exch_HideVoiceSignature.Value == 0 )|| q.exch_HideVoiceSignature == null )
&& !IsWet select q.exch_HideVoiceSignature;
question.FirstOrDefault().Dump();

如您所见,我可以将变量 IsWet 传递给 LINQ 查询以获得标准列表,并获取第一个列表的值。但是当我对第二个列表执行相同的操作时,我收到以下错误

'where' 条件无效。实体成员正在调用无效的属性或方法

4

3 回答 3

3

CRM LINQ 提供程序不支持您尝试的评估。它只支持评估where标准是评估实体字段。

这不是问题。由于您希望 LINQ 查询仅使用 where 子句 ifIsWet为 false (如果我错了,请纠正我。)所以我们只需进行评估以确定是否where应该添加该子句。然后执行您的查询。

   var question = from q in exch_questionSet
                  select q.exch_HideVoiceSignature;

    if (!IsWet)
    {
        question.Where(x => ((x.exch_HideVoiceSignature != null
                       && x.exch_HideVoiceSignature.Value == 0) || x.exch_HideVoiceSignature == null));
    }

    question.FirstOrDefault().Dump();
于 2013-11-13T03:33:18.773 回答
2

我经常面临这个问题。

尝试从 CRM 中“分离”(例如调用.ToArray())您的查询(当它是“清除”时),然后使用外部参数过滤查询。这应该会有所帮助。

var question = 
    (from q in exch_questionSet
    where (
    (q.exch_HideVoiceSignature != null && q.exch_HideVoiceSignature.Value == 0 ) || 
    q.exch_HideVoiceSignature == null )
    select q.exch_HideVoiceSignature
    ).ToArray().Where(q => !IsWet);


question.FirstOrDefault().Dump();

更新

如果您使用IsWet标志来控制条件块(从代码中的某一点启用和禁用它们),那么您可能会对名为PredicateBuilder的类感兴趣,它允许您动态构造谓词。

于 2013-11-12T20:20:58.490 回答
-1

仅仅因为我有一个包含许多其他连接等的现有查询,并且我想将这个附加参数传递给它,我最终使用了一个 var 语句,它将行转储到一个列表并在同一语句中应用 where 子句

bool IsWet =true ;
var question = ...existing query ...
select new {
...existing output ...,
Wet =q.exch_HideVoiceSignature != null &&  
     q.exch_HideVoiceSignature.Value == 119080001,
Voice = q.exch_HideVoiceSignature == null ||
        (q.exch_HideVoiceSignature != null && 
        q.exch_HideVoiceSignature.Value == 119080000)  ,
}
;

var qq = IsWet ? 
         question.ToList().Where(X=> X.Wet ) : 
         question.ToList().Where(X=> X.Voice );

qq.FirstOrDefault().Dump();
于 2013-11-14T15:24:13.190 回答