简体   繁体   中英

Additional information: The entity or complex type 'cb2.Models.ResultsVM' cannot be constructed in a LINQ to Entities query

How do I project the model fromo the first part of this query (Select p+> new...), into the ResultsVM viewmodel (Select(p => new ResultsVM....)?

When I try below, I get the error: Additional information: The entity or complex type 'cb2.Models.ResultsVM' cannot be constructed in a LINQ to Entities query.

I know I have to project to another anonymous type, so have to replace:

.Select(p => new ResultsVM with .Select(p => new - but my view is expecting a ResultsVM not an anonymouse type.

Any ideas how I fix this?

Thank you,

Mark

           var qas = db.QAs.Include(q => q.Analyst).Where(x => x.date >= from && x.date < to);

        var res = qas.GroupBy(p => p.Analyst.AgentName)
    .Select(p => new
    {
        AnalystId = p.Key,
        Analyst = p.FirstOrDefault().Analyst.AgentName,
        CorrectP = p.Where(x => x.priority == 1).Count(),
        WrongP = p.Where(x => x.priority == 0).Count(),
        CorrectA = p.Where(x => x.assignment == 1).Count(),
        WrongA = p.Where(x => x.assignment == 0).Count(),
        CorrectS = p.Where(x => x.solution == 1).Count(),
        WrongS = p.Where(x => x.solution == 0).Count()
    })
    .Select(p => new ResultsVM
    {
        AnalystId = p.AnalystId,
        Analyst = p.Analyst,
        CorrectP = p.CorrectP,
        WrongP = p.WrongP,
        Pp = p.CorrectP + p.WrongP != 0 ? p.CorrectP * 100.0 / (p.CorrectP + p.WrongP) : 0,
        CorrectA = p.CorrectA,
        WrongA = p.WrongA,
        Ap = p.CorrectA + p.WrongA != 0 ? p.CorrectA * 100.0 / (p.CorrectA + p.WrongA) : 0,
        CorrectS = p.CorrectS,
        WrongS = p.WrongS,
        Sp = p.CorrectS + p.WrongS != 0 ? p.CorrectS * 100.0 / (p.CorrectS + p.WrongS) : 0

    }).ToArray();

Results VM:

 public class ResultsVM
{
    public int Id { get; set; }
    public string AnalystId { get; set; }
    public string Analyst { get; set; }
    public int CorrectP { get; set; }
    public int WrongP { get; set; }
    public double Pp { get; set; }
    public int CorrectA { get; set; }
    public int WrongA { get; set; }
    public double Ap { get; set; }
    public int CorrectS { get; set; }
    public int WrongS { get; set; }
    public double Sp { get; set; }

}

What is strange is, this code works without erroring, and as far as I can see, is trying to do exactly the same thing:

            var res = scores2.GroupBy(p => p.AnalystId)
      .Select(p => new
      {
          AnalystId = p.Key,
          Analyst = p.FirstOrDefault().Analyst.AnalystName,
          score = p.Sum(x => x.Score),
          taskcount = p.Count()
      })
      .Select(p => new ObjectiveScoreVM
      {
          AnalystId = p.AnalystId,
          Analyst = p.Analyst,
          Score = p.taskcount != 0 ? p.score * 100.0 / p.taskcount : 0,
          TasksMet = p.score,
          TaskCount = p.taskcount
      })
      .ToArray();

thanks, Mark

When you make a LINQ to Entities Query, the compiler tries to convert the query to a query that works in the EF model. Sometimes, when you do this it fails because you're trying to do something that can't be done in the EF model.

In this case the offending operation is the second projection ( Select ). I can't identify the reason why this is happening. I'd need more info.

If you want to avoid this failure, you simply need to move the offending projection (second Select ) from LINQ to Entities to LINQ to Objects (which is much more flexible).

To do this, materialize the first part of you query, with ToList() or whatever else that "executes" the query and returns a collection of objects, excluding hte offending part (second Select ).

Now, when you work with the result of the first query, you are working with a collection of objects, thus you're using LINQ to Objects, which is much more flexible and will allow you to execute the previously faling projection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM