LINQ to Entities does not recognize the method 'Int32

后端 未结 2 1524
一向
一向 2020-12-19 07:37
public ActionResult ReadXMLDevices(int groupID)
{             
    var query = from k in XMLEntities.unassigneditems
    where k.DevOrAcc == true && k.Group          


        
相关标签:
2条回答
  • There is another easy way.

    First, load the data from database to memory:

    // ...
    var query_view = from i in query
                     select i;
    
    query_view.Load();
    // ...
    

    Then, do whatever you want with linq-to-objects, not L2E:

    var view_query_1 = from i in DbContext.myEntities.Local
                     select new GetFreeDevices
                     {
                         MArticleNumber = i.ArticleNumber,
                         MFirmware = i.Firmware,
                         MGroup = i.Group,
                         MName = i.Name,
                         MSoftware = i.SoftwareVersion,
                         SA = GetNumberOfDevices(i.ArticleNumber,2),   //
                         STH = GetNumberOfDevices(i.ArticleNumber,3),  // These are now ok!
                         SASTH = GetNumberOfDevices(i.ArticleNumber,7) //
                     };
        return PartialView(view_query_1.AsEnumerable());
    

    You can use this trick for any methods that L2E doesn't support.

    0 讨论(0)
  • 2020-12-19 08:06

    You can split your query in two to make sure the list is in-memory before calling the GetNumberOfDevices() method. You can make a query in-memory by converting the results to a List, or in this case a List<GetFreeDevices>. That way, LinQ to Entities does not have to do any translations to and from XML and you can use your GetNumberOfDevices() method.

    var view_query = (from i in query
         select new GetFreeDevices
         {
            MArticleNumber = i.ArticleNumber,
            MFirmware = i.Firmware,
            MGroup = i.Group,
            MName = i.Name,
            MSoftware = i.SoftwareVersion
    
         }).ToList();
    var result_query = from i in query
         select new GetFreeDevices
         {
            MArticleNumber = i.MArticleNumber,
            MFirmware = i.MFirmware,
            MGroup = i.MGroup,
            MName = i.MName,
            MSoftware = i.MSoftware,
            SA = GetNumberOfDevices(i.MArticleNumber,2),
            STH = GetNumberOfDevices(i.MArticleNumber,3),
            SASTH = GetNumberOfDevices(i.MArticleNumber,7)
         };
    return PartialView(result_query);
    

    Please note that the last statement requires the PartialView to accept a list or IEnumerable instead of an IQueryable.

    0 讨论(0)
提交回复
热议问题