Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

c# - LINQ to Entities does not recognize the method 'Int32

public ActionResult ReadXMLDevices(int groupID)
{             
    var query = from k in XMLEntities.unassigneditems
    where k.DevOrAcc == true && k.Group == groupID
    select k;

    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,
                         SA = GetNumberOfDevices(i.ArticleNumber,2),
                         STH = GetNumberOfDevices(i.ArticleNumber,3),
                         SASTH = GetNumberOfDevices(i.ArticleNumber,7)
                     };
    return PartialView(view_query);
}

public int GetNumberOfDevices(string artNo,int loc)
{
    var num_dev = (from k in XMLEntities.deviceview
                   where k.Reserved == false && k.Sold == false && k.LocationNameId == loc && k.ArticleNumber == artNo
                   select k).Count();
    return num_dev;
}

Error:

LINQ to Entities does not recognize the method 'Int32 GetNumberOfDevices(System.String, Int32)' method, and this method cannot be translated into a store expression. How to resolve this???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...