LINQ to Entities does not recognize method

孤街醉人 提交于 2019-12-23 18:16:42

问题


I have a long Linq To Entities query:-

reports = db.CallProfileDailies
    .Join(db.ReportDailyTotals, 
          cpd => cpd.Date, 
          rdt => rdt.Date, 
          (cpd, rdt) => new { cpd = cpd, rdt = rdt })
    .Where(a => a.cpd.Skill == a.rdt.Skill)
    .Join(db.SummaryIntervalTotals, 
          a => a.rdt.Date, 
          sit => sit.Date,
          (a, sit) => new { cpd = a.cpd, rdt = a.rdt, sit = sit })
    .Where(a => a.rdt.Skill == a.sit.Skill)
    .Select((a, i) => new ReportModel
    {
        AverageAbandonDelay = a.sit.AvgAbanTime,
        AverageAfterCallWorkTime = a.sit.AvgAcwTime,
        AverageHandleTime = (a.sit.AvgAcwTime + a.sit.AvgAcdTime),
        AverageSpeedOfAnswer = a.sit.AvgSpeedAns,
        AverageTalkTime = a.sit.AvgAcdTime,
        CallsAnswered = a.sit.AcdCalls,
        Date = a.sit.Date.ToString(),
        MaximumDelay = a.sit.MaxDelay,
        PercentageAbandon = (a.sit.AbanCalls / (a.sit.AcdCalls + a.sit.AbanCalls)),
        TotalCallsAbandon = a.sit.AbanCalls,
        TotalCallsOffered = (a.sit.AcdCalls + a.sit.AbanCalls),
        TotalHandleTime = (a.rdt.HoldTime + a.rdt.AcdTime + a.rdt.AcwTime)
    }).Take(5).ToList();

I am getting the following error at runtime:-

LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1[ExpediaReports.Models.ReportModel] Select[<>f_AnonymousType1`3,ReportModel](System.Linq.IQueryable`1[<>f_AnonymousType1`3[ExpediaReports.CallProfileDaily,ExpediaReports.ReportDailyTotal,ExpediaReports.SummaryIntervalTotal]], System.Linq.Expressions.Expression`1[System.Func`3[<>f__AnonymousType1`3[ExpediaReports.CallProfileDaily,ExpediaReports.ReportDailyTotal,ExpediaReports.SummaryIntervalTotal],System.Int32,ExpediaReports.Models.ReportModel]])' method, and this method cannot be translated into a store expression.

I just want to understand what does this error mean. I am not even able to read which method it (Linq To Entities) is not able to recognize.

How to read this error and identify the unrecognized method so that I can change my query accordingly ?

What does these characters mean here : ` <> ' etc.


回答1:


"this method cannot be translated into a store expression" means that Linq to Entities can't translate your query to SQL.

This often happens when you have something in your .Select() function that tries to manipulate the results in a way that can't be directly translated to SQL, such as when you run a function. I'd bet that the "a.sit.Date.ToString()" line is causing the problem. Just return the date and format it after you've called .ToList() and I'll bet the problem will go away.

It can be frustrating -- Linq to SQL was a lot better at running queries like this where you had complex functions in your .Select() function. Keep in mind that Linq to Entities is very limited on the functions it will translate over to SQL.




回答2:


Using ToString really doesn't work but problem here was .Select((a, i) => This version of Select is not supported by Linq-to-entities. Exception says it.



来源:https://stackoverflow.com/questions/5484675/linq-to-entities-does-not-recognize-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!