附加信息:无法在LINQ to Entities查询中构造实体或复杂类型“cb2.Models.ResultsVM”

时间:2014-01-10 11:28:00

标签: c# linq entity-framework linq-to-entities

如何将模型从此查询的第一部分(选择p +> new ...)投影到ResultsVM视图模型中(选择(p => new ResultsVM ....)?

当我在下面尝试时,我收到错误:附加信息:无法在LINQ to Entities查询中构造实体或复杂类型“cb2.Models.ResultsVM”。

我知道我必须投射到另一个匿名类型,所以必须替换:

带有.Select(p => new ResultsVM

.Select(p => new - 但我的观点是期望结果视频不是匿名类型。

我是如何解决这个问题的?

谢谢,

标记

           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();

结果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; }

}

奇怪的是,这段代码没有错误,而且据我所知,它试图做同样的事情:

            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();

谢谢,Mark

1 个答案:

答案 0 :(得分:0)

当您创建LINQ to Entities查询时,编译器会尝试将查询转换为在EF模型中工作的查询。有时,当你这样做时,它会失败,因为你试图做一些在EF模型中无法完成的事情。

在这种情况下,违规操作是第二次投影(Select)。我无法确定发生这种情况的原因。我需要更多信息。

如果你想避免这种失败,你只需要将违规投影(第二个Select)从LINQ移动到实体到LINQ到对象(这要灵活得多)。

要执行此操作,请使用ToList()或“执行”查询的其他任何内容来实现查询的第一部分,并返回对象集合,不包括违反部分(第二个Select)。

现在,当您使用第一个查询的结果时,您正在处理一组对象,因此您正在使用LINQ to Objects,它更灵活,并允许您执行以前的faling投影。

相关问题