I have this code in MVC c#, which its concept is ok. the "x.Letter.Kind" is numbber which I want to translated to a text when I am creating the list. So I wrote a function (LetterType) ,which takes a Byte and returns its associated text. But when the program runs, it gives me an error which says:
{"LINQ to Entities does not recognize the method 'System.String LetterType(Byte)' method, and this method cannot be translated into a store expression."}
I cannnot understand what it means. can somebody help me on this issue? Thank you
public JsonResult GetLetterList(DataTablesParam param)
{
int fld_id = 23571;
int userId = 1454;
int totalCount = 0;
List<ViewLetterInformation> list = new List<ViewLetterInformation>();
using (WebECartableEntities db = new WebECartableEntities())
{
int pageNo = 1;
if (param.iDisplayStart >= param.iDisplayLength)
{
pageNo = param.iDisplayStart / param.iDisplayLength + 1;
}
totalCount = db.Referrings.Where(x => x.usr_Receiver__Id == userId && x.fld_Id == fld_id).Count();
list = db.Referrings.Where(x => x.usr_Receiver__Id == userId && x.fld_Id == fld_id).OrderBy(x => x.Letter_Id).Skip((pageNo - 1) * param.iDisplayLength).Take(param.iDisplayLength).Select(x => new ViewLetterInformation
{
Letter_Id = x.Letter_Id,
ProjectCode = x.Letter.Project.Code,
LetterType = LetterType(x.Letter.Kind) //This line gives error
Ref_subject = x.Subject
}).ToList();
return Json(new
{
aaData = list,
sEcho = param.sEcho,
iTotalDisplayRecords = totalCount,
iTotalRecords = list.Count()
}, JsonRequestBehavior.AllowGet);
}
}
}
private string LetterType(byte LetterType)
{
string result = "";
switch (LetterType)
{
case 0:
case 4:
case 8:
result = "Import";
break;
case 1:
case 5:
case 9:
result = "Export";
break;
case 2:
case 6:
case 10:
result = "Internal";
break;
default:
result = "";
break;
}
return result;
}
if I use
LetterType = x.Letter.Kind.toString()
instead of
LetterType = LetterType(x.Letter.Kind)
it works fine, but I need its equivalent text data.
Thank you
AsEnumerablebefore theSelectthat will cause your projection to occur in memory where you don't have to worry about translating your code to SQL. - juharrlistfrom, then applyLetterTypefilter. Linq to entities will not be able to convert your method into the SQL statement. - SᴇMAsEnumerableyou're telling to stop translating to SQL at that point and do the rest of the Linq code in memory after fetching the data. - juharr