2011-12-28 27 views
1

我正在開發ac#項目並編寫一個LINQ查詢,在此查詢中我需要創建一個組,但我知道我想使用該組類型,但是小組給了我一些麻煩,因爲我無法將它轉換爲我想要的類型。將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 
}); 

回答

0

您還沒有表現出任何你的類型的簽名。我們只能猜測你想要的類型的外觀。下次您提問時,請確保您提供了SSCCE

總之,根據你的例子這裏是這種自定義類型應該如何看起來像:

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

其中Departmentdepartments集合中的元素的類型和它假定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 
    }; 
+0

就像一個魅力,由於 – 2011-12-28 11:48:49

相關問題