简体   繁体   中英

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

I have this erun time error exception

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.

I tried what ever I can to fixed it but no use. I think the exception coming from list can someone help me to fix it

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;
        }

You are trying to embed a .NET library function to your EF query. As it is not translatable to SQL, this is not supported.

You must rewrite your query, without using the .ToDictionary().

This particular case it may not to hard. Please revisit if the projection ToDictionary() is necessary at all. You can safely write SQL translatable projection by using:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM