在Linq to SQL上返回什么数据类型?

Kee*_*isk 3 c# linq-to-sql c#-3.0

我想弄清楚返回类型.看起来ToList()返回一个List,但我不确定它是什么类型.

 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();
Run Code Online (Sandbox Code Playgroud)

Pra*_*ana 5

它是返回的匿名类型列表,其中包含id和type作为属性.

当您在linq查询中编写Select new时,它会创建一个匿名类型,其中包含您在select new {}中添加的属性.

在此输入图像描述

完整aritlce:SQL到LINQ(视觉表示)

编辑

@KeelRisk - 你不能从方法中返回匿名类型列表...如果你只想返回id而不是修改查询select to" List<int> lstid= (.....Select h.id).ToList<int>();"而不是返回lstid..will do you to you

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(); 
Run Code Online (Sandbox Code Playgroud)