0
votes

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

1
If you put an AsEnumerable before the Select that will cause your projection to occur in memory where you don't have to worry about translating your code to SQL. - juharr
You can get your list from, then apply LetterType filter. Linq to entities will not be able to convert your method into the SQL statement. - SᴇM
Is LetterType an enumeration? If so cast instead of using kind. Try LetterType = (Put enumeration type here, not the actual enumeration name)LetterType - jdweng
@jdweng: it is not - nnmmss
The issue is that EF is trying to convert your C# code to SQL. It knows how to do that for most C# operators and common libraries, but it doesn't for your custom method. So by using AsEnumerable you'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

1 Answers

-1
votes

Try bringing the entity into memory by calling .ToList() like this:

list = db.Referrings.ToList().Where(...).Select(...).ToList();