我正在做一个c#项目并编写一个LINQ查询,在这个查询中我需要创建一个组,但我知道我想使用该组类型,但是组的类型给了我一些麻烦,因为我无法将其转换为我想要的类型。
My Query is
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
});
您还没有显示任何类型的签名。我们只能猜测你想要的类型是什么样子。下次你问问题时,一定要提供SSCCE。
无论如何,根据你的例子,这个自定义类型应该是这样的:
public class MyCustomType
{
public IGrouping<int, Department> Department { get; set; }
}
,其中Department
为departments
集合内元素的类型,且假定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
};