2016-05-16 62 views
0

這似乎是在編譯下面的linq查詢時出現錯誤。我已經閱讀了一些類似的問題,但都強調使用.Any()函數而不是.Contains()..但是,儘管如此,似乎編譯該語句時出現錯誤,並且會記錄一個異常。System.NotSupportedException:無法創建一個常量值的type_.Only原語和......此上下文

這是結: -

int result=0; 
result = (from u in objContext.CLG_challengeMaster 
      where u.appId == applicationId 
      && objContext.CLG_taskDetail.Any(e => e.challengeId == u.id) 
      && !objContext.CLG_challengeDetail.Any(e => e.challengeId == u.id && (e.userId == null && e.groupId == null && e.appGroupId == null && e.roleId == null)) 
      select u).Count(); 

這是錯誤日誌:

Message: System.NotSupportedException: Unable to create a constant value of type 'DataAccess.Entities.Models.CLG_taskDetail'. Only primitive types or enumeration types are supported in this context. 
    at System.Data.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq) 
    at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) 
    at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) 
    at System.Data.Objects.ELinq.ExpressionConverter.NewArrayInitTranslator.<>c__DisplayClass0.<TypedTranslate>b__1(Expression e) 
    at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() 
    at System.Data.Common.CommandTrees.ExpressionBuilder.Internal.EnumerableValidator`3.Validate(IEnumerable`1 argument, String argumentName, Int32 expectedElementCount, Boolean allowEmpty, Func`3 map, Func`2 collect, Func`3 deriveName) 
    at System.Data.Common.CommandTrees.ExpressionBuilder.Internal.EnumerableValidator`3.Validate()......... 

誰能plz幫助。

在這一行
+0

最後只好將它分解和重建它: ( –

回答

0

如果&& objContext.CLG_taskDetail.Any(e => e.challengeId == u.id) CLG_taskDetail可能是null,則將該行更改爲:

&& (objContext.CLG_taskDetail.Any() && objContext.CLG_taskDetail.Where(e => e.challengeId == u.id)) 

否則將其更改爲使用「去哪兒」條款

+0

不,這是行不通的編譯錯誤說:嚴重性\t代碼\t說明\t項目\t文件\t線 錯誤\t CS0019 \t操作「&&」 C註釋應用於類型'bool'和'IQueryable '的操作數 –

+0

您可以嘗試以下操作,因爲我認爲上述問題是您尚未加入表格。 'objContext.CLG_challengeMaster.Where(u => u.appId == applicationId && u.CLG_taskDetail.Any(e => e.ChallengeId == u.id)&&!u.CLG_challengeDetail.Any(f => f.challengeid = = u.id && f.userId == null && f.groupId == null && f.appGroupId == null && f.roleId == null))。count();'不認爲你需要這個選擇 – bilpor

相關問題