将Linq子查询转换为用户定义的类型

时间:2012-01-05 05:15:51

标签: c# linq entity-framework-4.1

我有一个linq查询,它还有一个子查询,我想将该查询的结果存储到用户定义的类型中,我的查询是

var val = (from emp in Employees
                   join dept in Departments
                   on emp.EmployeeID equals dept.EmployeeID
                   select new Hello
                   {

                       EmployeeID = emp.EmployeeID
                       Spaces = (from order in Orders
                               join space in SpaceTypes
                               on order.OrderSpaceTypeID equals space.OrderSpaceTypeID
                               where order.EmployeeID == emp.EmployeeID                                   group new { order, space } by new { order.OrderSpaceTypeID, space.SpaceTypeCode } into g
                               select new
                               {
                                   ID = g.Key.SpaceTypeID,
                                   Code = g.Key.SpaceTypeCode,
                                   Count = g.Count()
                               })
                   }).ToList();

我的Hello类的定义是

public class Hello
{      
    public IEnumerable<World> Spaces { get; set; }
    public int PassengerTripID { get; set; }      
}

我的世界级的定义是

 public class World
{
    public int ID { get; set; }
    public string Code { get; set; }
    public int Count { get; set; }
}

1 个答案:

答案 0 :(得分:1)

您正在创建匿名对象,但您需要指定类型名称World

 select new World
          {
           ID = g.Key.SpaceTypeID,
           Code = g.Key.SpaceTypeCode,
          Count = g.Count()
           })