Вопрос

I am working on a c# project and writing a LINQ query, in this query I need to create a group, but I know I want to use that group type, but the type of the group gives me some trouble as I am not able to cast it to my desired type.

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
});
Это было полезно?

Решение

You haven't shown the signature of any of your types. We can only be guessing how your desired type might look. Next time you ask a question make sure you provide an SSCCE.

Anyway, according to your example here's how this custom type should look like:

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

where Department is the type of elements inside the departments collection and it assumes that EmpID is of type integer.

Example:

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
    };
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top