元组与匿名类型与 Expando 对象。 (关于 LINQ 查询)

发布于 2024-09-04 22:44:32 字数 350 浏览 2 评论 0原文

我是一个终于开始理解匿名类型的初学者。

(参见旧帖子 匿名 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬 2024-09-11 22:44:33

元组和 Expando 对象通常不在 LINQ 中使用。它们与匿名类型有很大不同。

匿名类型通常用于“塑造”LINQ 查询;例如,您可以定义一个具有 string Name 属性和 int Age 属性的类型。

元组是仅充当“对”或“三元组”类型结构的类型。例如,可以定义 Tuple,但属性名称命名为 Item1Item2,而不是 姓名年龄。元组通常不用于塑造 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 an int 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 named Item1 and Item2, not Name and Age. 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.

池予 2024-09-11 22:44:33

您没有说明问题的上下文,因此我将同时回答 LinqToObjects 和 LinqToSql。

在 LinqToObjects 中,假设您有一个 List;来源

  //Straight projection.
  //no new instances are created when query is evaluated.
IEnumerable<Customer> result =
  from c in source where c.Name == "Bob"
  select c;

  //Different Type projection
  //a new instance of CustomerName is created
  // for each element in the result when the query is evaluated.
IEnumerable<CustomerName> result =
  from c in source where c.Name == "Bob"
  select new CustomerName() {Name = c.Name};

  //Anonymous Type Projection    
  //a new instance of an anonymous type is created
  // for each element in the result when the query is evaluated.
  //You don't have access to the type's name
  // since the compiler names the type,
  // so you must use var to talk about the type of the result.
var result =
  from c in source where c.Name == "Bob"
  select new {Name = "Bob"};

  //Tuple Projection (same as Different Type Projection)
  //a new instance of Tuple is created
  // for each element in the result when the query is evaluated.
IEnumerable<Tuple<string, int>> result = 
  from c in source where c.Name == "Bob"
  select new Tuple<string, int>(){First = c.Name, Second = c.Id};

在 LinqToSql 中,假设您有一个 IQueryabledb.Customers

  //Straight projection
  //when the query is resolved
  // DataContext.Translate<Customer> is called
  // which converts the private dbReader into new Customer instances.
IQueryable<Customer> result =
  from c in db.Customers where c.Name == "Bob"
  select c;

  //Different Type Projection
  //when the query is resolved
  // DataContext.Translate<CustomerName> is called
  // which converts the private dbReader into new CustomerName instances.
  // 0 Customer instances are created.
IQueryable<Customer> result =
  from c in db.Customers where c.Name == "Bob"
  select new CustomerName() {Name = c.Name};

  //Different Type Projection with a twist
  //when the query is resolved
  // DataContext.Translate<CustomerGroup> is called
  // which converts the private dbReader into new CustomerGroup instances.
  // 0 Customer instances are created.
  //the anonymous type is used in the query translation
  // yet no instances of the anonymous type are created.
IQueryable<Customer> result =
  from c in db.Customers
  group c by new {c.Name, TheCount = c.Orders.Count()} into g
  select new CustomerGroup()
  {
    Name = g.Key.Name,
    OrderCount = g.Key.TheCount,
    NumberInGroup = g.Count()
  };

好的,现在就足够了。

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.

  //Straight projection.
  //no new instances are created when query is evaluated.
IEnumerable<Customer> result =
  from c in source where c.Name == "Bob"
  select c;

  //Different Type projection
  //a new instance of CustomerName is created
  // for each element in the result when the query is evaluated.
IEnumerable<CustomerName> result =
  from c in source where c.Name == "Bob"
  select new CustomerName() {Name = c.Name};

  //Anonymous Type Projection    
  //a new instance of an anonymous type is created
  // for each element in the result when the query is evaluated.
  //You don't have access to the type's name
  // since the compiler names the type,
  // so you must use var to talk about the type of the result.
var result =
  from c in source where c.Name == "Bob"
  select new {Name = "Bob"};

  //Tuple Projection (same as Different Type Projection)
  //a new instance of Tuple is created
  // for each element in the result when the query is evaluated.
IEnumerable<Tuple<string, int>> result = 
  from c in source where c.Name == "Bob"
  select new Tuple<string, int>(){First = c.Name, Second = c.Id};

In LinqToSql, suppose you have an IQueryable<Customer> db.Customers

  //Straight projection
  //when the query is resolved
  // DataContext.Translate<Customer> is called
  // which converts the private dbReader into new Customer instances.
IQueryable<Customer> result =
  from c in db.Customers where c.Name == "Bob"
  select c;

  //Different Type Projection
  //when the query is resolved
  // DataContext.Translate<CustomerName> is called
  // which converts the private dbReader into new CustomerName instances.
  // 0 Customer instances are created.
IQueryable<Customer> result =
  from c in db.Customers where c.Name == "Bob"
  select new CustomerName() {Name = c.Name};

  //Different Type Projection with a twist
  //when the query is resolved
  // DataContext.Translate<CustomerGroup> is called
  // which converts the private dbReader into new CustomerGroup instances.
  // 0 Customer instances are created.
  //the anonymous type is used in the query translation
  // yet no instances of the anonymous type are created.
IQueryable<Customer> result =
  from c in db.Customers
  group c by new {c.Name, TheCount = c.Orders.Count()} into g
  select new CustomerGroup()
  {
    Name = g.Key.Name,
    OrderCount = g.Key.TheCount,
    NumberInGroup = g.Count()
  };

Ok, that's enough for now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文