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???
Best Solution
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 aList
, or in this case aList<GetFreeDevices>
. That way, LinQ to Entities does not have to do any translations to and from XML and you can use yourGetNumberOfDevices()
method.Please note that the last statement requires the PartialView to accept a list or
IEnumerable
instead of anIQueryable
.