1

我正在处理 ac# 项目并编写 LINQ 查询,在此查询中我需要创建一个组,但我知道我想使用该组类型,但组的类型给我带来了一些麻烦,因为我无法将其转换为我想要的类型。

我的查询是

from emp in employees
join dept in departments
on emp.EmpID equals dept.EmpID
group dept by dept.EmpID into groupSet
select new mycustomType
{
    Department = groupSet
});
4

1 回答 1

0

您还没有显示任何类型的签名。我们只能猜测您想要的类型的外观。下次您提出问题时,请确保提供SSCCE

无论如何,根据您的示例,此自定义类型应如下所示:

public class MyCustomType
{
    public IGrouping<int, Department> Department { get; set; }
}

whereDepartmentdepartments集合内元素的类型,它假定它EmpID是整数类型。

例子:

IEnumerable<Employee> employees = ...
IEnumerable<Department> departments = ...

IEnumerable<MyCustomType> result = 
    from emp in employees
    join dept in departments
    on emp.EmpID equals dept.EmpID
    group dept by dept.EmpID into groupSet
    select new MyCustomType
    {
        Department = groupSet
    };
于 2011-12-28T11:40:09.170 回答