元组与匿名类型与 Expando 对象。 (关于 LINQ 查询)
我是一个终于开始理解匿名类型的初学者。
(参见旧帖子 匿名 linq 查询选择的返回类型是什么?发送回此数据的最佳方式是什么?)
因此,在 LINQ 查询中,您可以在 linq 中形成所需的返回值类型查询对吗?看来做到这一点的方法是匿名类型,对吧?
有人可以向我解释一下我是否以及何时可以使用 Tuple/Expando 对象来代替吗?他们看起来都很相似?
I am a beginner who finally started understanding anonymous types.
(see old post What is the return type for a anonymous linq query select? What is the best way to send this data back?)
So in LINQ queries you form the type of return value you want within the linq query right? It seems the way to do this is anonymous type right?
Can someone explain to me if and when I could use a Tuple/Expando object instead? They all seem very simliar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
元组和 Expando 对象通常不在 LINQ 中使用。它们与匿名类型有很大不同。
匿名类型通常用于“塑造”LINQ 查询;例如,您可以定义一个具有
string Name
属性和int Age
属性的类型。元组是仅充当“对”或“三元组”类型结构的类型。例如,可以定义
Tuple
,但属性名称命名为Item1
和Item2
,而不是姓名
和年龄
。元组通常不用于塑造 LINQ 查询,因为这些属性名称使代码不太清晰。ExpandoObject 完全不同。它允许您在运行时向现有对象添加属性。
Tuples and Expando objects are not normally used in LINQ. They're both quite different than anonymous types.
Anonymous types are commonly used to "shape" LINQ queries; for example, you can define a type that has a
string Name
property and anint Age
property.Tuples are types that only act as "pair" or "triplet" kinds of structures. For example a
Tuple<string, int>
can be defined, but the names of the properties are namedItem1
andItem2
, notName
andAge
. Tuples are not normally used to shape LINQ queries because these property names make the code less clear.ExpandoObject is totally different. It allows you to add properties at runtime to an existing object.
您没有说明问题的上下文,因此我将同时回答 LinqToObjects 和 LinqToSql。
在 LinqToObjects 中,假设您有一个
List;来源
。在 LinqToSql 中,假设您有一个
IQueryabledb.Customers
好的,现在就足够了。
You don't state the context of your question, so I'll answer both LinqToObjects and LinqToSql.
In LinqToObjects, suppose you have a
List<Customer> source
.In LinqToSql, suppose you have an
IQueryable<Customer> db.Customers
Ok, that's enough for now.