select student.ID;
下面的示例演示如何使用匿名类型只返回每个源元素的符合指定条件的属性子集。
private static void QueryByScore()
{
// Create the query. var is required because
// the query produces a sequence of anonymous types.
var queryHighScores =
from student in students
where student.ExamScores[0] > 95
select new { student.FirstName, student.LastName };
// Execute the query.
foreach (var obj in queryHighScores)
{
// The anonymous type's properties were not named. Therefore
// they have the same names as the Student properties.
Console.WriteLine(obj.FirstName + ", " + obj.LastName);
}
}
输出:
Adams, Terry
Fakhouri, Fadi
Garcia, Cesar
Omelchenko, Svetlana
Zabokritski, Eugene
请注意,如果未指定名称,则匿名类型将使用源元素的名称作为其属性名称。若要为匿名类型中的属性指定新名称,请按如下方式编写 select 语句:
select new { First = student.FirstName, Last = student.LastName };
如果您在上一个示例中这样做,则 Console.WriteLine 语句也必须更改:
Console.WriteLine(student.First + " " + student.Last);
注:相关教程知识阅读请移步到c#教程频道。










