2012-03-14 57 views
3

我想弄清楚返回類型。它看起來像ToList()返回一個列表,但我不知道什麼類型。這個Linq to SQL返回什麼數據類型?

var id = (from h in db.t_ref_harvest_type 
        where h.manner_of_take_id == methodOfTakeId && 
       parsedSeasons.Contains((int)h.season) && h.active == true 
        select new { h.id, h.harvest_type }).ToList(); 

回答

5

它的匿名類型的列表返回其爲具有標識和類型在其財產。

當您編寫在linq查詢中選擇新建時,它將使用您在選擇新建{}中已經完成的屬性創建匿名類型。

enter image description here

完全aritlce:SQL to LINQ (Visual Representation)

編輯

@KeelRisk - 你不能從方法返回匿名類型的列表...如果你只是想返回ID不是修改查詢選擇「List<int> lstid= (.....Select h.id).ToList<int>();」,並且比回程客戶要做的更好

List<int> lstid = (from h in db.t_ref_harvest_type     
where h.manner_of_take_id == methodOfTakeId && parsedSeasons.Contains((int)h.season) 
&& h.active == true     
select h.id).ToList(); 
+0

好的,然後我的方法應該返回什麼類型?它只是返回var id。或者更好的說,我該如何返回一個匿名類型列表? – KeelRisk 2012-03-14 04:57:43

+0

@KeelRisk - 你不能從方法中返回匿名類型列表...如果你只是想返回id而不是修改查詢,請選擇「列表 lstid =(..... Select h.id).ToList () ;」和回報lstid..will對你 – 2012-03-14 05:00:39

+0

好吧,我想我需要循環的結果,並拿出id。謝謝 – KeelRisk 2012-03-14 05:02:09