当前位置: 首页> 健康> 养生 > 天津基础设施建设_企点qq_品牌推广服务_开网店怎么开 新手无货源

天津基础设施建设_企点qq_品牌推广服务_开网店怎么开 新手无货源

时间:2025/7/10 21:41:37来源:https://blog.csdn.net/m0_72813396/article/details/144572393 浏览次数:0次
天津基础设施建设_企点qq_品牌推广服务_开网店怎么开 新手无货源

当下列两个条件都满足时,可在查询表达式中使用匿名类型:

  • 只想返回每个源元素的某些属性。
  • 无需在执行查询的方法的范围之外存储查询结果。

如果只想从每个源元素中返回一个属性或字段,则只需在 select 子句中使用点运算符。 例如,若要只返回每个 student 的 ID,可以按如下方式编写 select 子句:

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 studentswhere student.ExamScores[0] > 95select 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);}
}
/* Output:
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);
编译代码

要运行此代码,请使用 System.Linq 的 using 指令将该类复制并粘贴到 C# 控制台应用程序中。

关键字:天津基础设施建设_企点qq_品牌推广服务_开网店怎么开 新手无货源

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: