无法隐式转换列表

时间:2018-11-08 08:58:30

标签: c# entity-framework

我想获取员工的签到状态,但是当我尝试将其转换为列表时,它只是让我返回其中一个,FirstFirstOrDefault,它告诉我:< / p>

  

无法将List <>转换为..Data.Entities.Checkinout

EmployeeCollection = (from userinfo in context.Userinfo
   join department in context.Dept on userinfo.Deptid equals department.Deptid
   where id == userinfo.Userid
   select new Employee()
   {
     Name = userinfo.Name,
     Department = department.DeptName,
     CardNumber = userinfo.CardNum,
     Status = userinfo.UserFlag.ToString(),
     ActualCheckinStatuse = (from checkinout in context.Checkinout
         join status in context.Status on checkinout.CheckType equals status.Statusid
         where checkinout.Userid == userinfo.Userid
         orderby checkinout.CheckTime descending
         select new Checkinout
         {
           CheckStatus = status.StatusText,
           CheckTime = checkinout.CheckTime
         }).First()
   }
).ToList()

1 个答案:

答案 0 :(得分:0)

部分代码如下:

       new Employee()
{
  Name = ...,
  Department = ...,
  CardNumber = ...,
  Status = ...,
  ActualCheckinStatuse = (from checkinout in context.Checkinout
      join status in context.Status on checkinout.CheckType equals status.Statusid
      where checkinout.Userid == userinfo.Userid
      orderby checkinout.CheckTime descending
      select new Checkinout
      {
        CheckStatus = status.StatusText,
        CheckTime = checkinout.CheckTime
      }).First()
}

在这里,表达式(from checkinout in ... select new Checkinout { ... })将是Checkinout实例的枚举(或序列,如果您愿意的话)。如上所述,当您对此执行.First()时,结果将是Checkinout类型的一个实例。

据我了解,编译效果很好。这是因为您在此初始值设定项中分配的属性ActualCheckinStatuse具有相同的声明类型Checkinout

如果在此括号(from checkinout in ... select new Checkinout { ... })之后附加了.ToList(),那么您得到的是.net Checkinout中的所有List<>实例。在记忆中。当然,您不能将其分配给ActualCheckinStatuse,因为类型不兼容。不能将Checkinout中的一个ActualCheckinStatuse设置为此类List<>的整个Checkinout