簡體   English   中英

LINQ to Entities無法識別方法'System.Collections.Generic.Dictionary`

[英]LINQ to Entities does not recognize the method 'System.Collections.Generic.Dictionary`

我有這個錯誤的時間錯誤異常

LINQ to Entities does not recognize the method
 'System.Collections.Generic.Dictionary`2[System.String,System.Collections.Generic.List`1[Project.Model.Value]]
ToDictionary[Value,String,List`1](System.Collections.Generic.IEnumerable`1[Project.Model.Value],
System.Func`2[Project.Model.Value,System.String],
System.Func`2[Project.Model.Value,System.Collections.Generic.List`1[Project.Model.Value]])'
method, and this method cannot be translated into a store expression.

我盡力修復它但沒用。 我認為來自列表的異常可以幫助我解決它

public IEnumerable<ItemManagement> getItemsForFormType(string formType)
        {
            using (var db = new AthenaContext())
            {

                List<Value> dropDownListValue = (from val in db.Values
                                                 where val.ParentId == (from va in db.Values
                                                                        where
                                                                            va.ParentId
                                                                            == (from value3 in db.Values
                                                                                where value3.Name == formType
                                                                            select value3.RecordId).FirstOrDefault()
                                                                    select va.RecordId).FirstOrDefault()
                                             select val).ToList();
            var result = (from value1 in db.Values
                          where value1.Name == formType
                          select
                              new ItemManagement
                              {

                                  FormType = value1.Name,
                                  RecordID = value1.RecordId,
                                  FormControllerNames =
                                  (from va in db.Values
                                   where va.ParentId == (from value3 in db.Values where value3.Name == formType select value3.RecordId).FirstOrDefault()
                                   select va).ToDictionary(va => va.Name, va => dropDownListValue)
                              }).ToList();
            return result;
        }

您正在嘗試將.NET庫函數嵌入到EF查詢中。 因為它不能翻譯成SQL,所以不支持。

您必須重寫查詢,而不使用.ToDictionary()。

這種特殊情況可能並不難。 如果根本需要投影ToDictionary(),請重新訪問。 您可以使用以下方法安全地編寫SQL可翻譯投影:

.Select( new { anyName: <expression>, otherName: <otherExpression>, etc })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM