我想用实际的类型定义替换“ var”。我相信它会返回IEnumerable <>,但是我不知道该为T放置什么?我尝试使用GetType()进行调试,但仍然没有得到...
var LinqResults = from row in dt.AsEnumerable()
orderby row.Field<string>("Category"), row.Field<int>("WorkOrderVersion")
group row by new { Category = row.Field<string>("Category"), WorkOrderVersion = row.Field<int>("WorkOrderVersion") } into grp
select new
{
Category = grp.Key.Category,
WorkOrderVersion = grp.Key.WorkOrderVersion,
};
答案 0 :(得分:3)
您不能将匿名类型的使用与特定类型结合使用,因为匿名类型不会为您提供要在T
中放入IEnumerable<T>
的名称。实际上,使用匿名类型是将var
首先添加到C#语言的重要用例。
您可以为结果定义一个命名类型,就是这样:
class VersionedWorkOrder {
public string Category { get; set; }
public int WorkOrderVersion { get; set; }
}
IEnumerable<VersionedWorkOrder> linqResults = from row in dt.AsEnumerable()
orderby row.Field<string>("Category"), row.Field<int>("WorkOrderVersion")
group row by new { Category = row.Field<string>("Category"), WorkOrderVersion = row.Field<int>("WorkOrderVersion") } into grp
select new VersionedWorkOrder {
Category = grp.Key.Category,
WorkOrderVersion = grp.Key.WorkOrderVersion
};