Linq的匿名声明 [英] Anonymous declaration in Linq

查看:63
本文介绍了Linq的匿名声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面给出的链接中,我无法理解他们给出的示例。

http://msdn.microsoft.com/en-us/library/bb534675.aspx [ ^ ]



我查看此链接以了解有关的基本概念加入对象上下文,我的问题是



In the given link below, I couldn't understand the example that is given by them.
http://msdn.microsoft.com/en-us/library/bb534675.aspx[^]

I looked into this link to have a basic idea about Join in Object Context, and my question is

var query = petsList.GroupBy(
                  pet => Math.Floor(pet.Age),
                  (age, pets) => new
                  {
                      Key = age,
                      Count = pets.Count(),
                      Min = pets.Min(pet => pet.Age),
                      Max = pets.Max(pet => pet.Age)
                  });



(示例用于链接)

在上面,年龄和宠物代表什么?


(example that is used in the link)
In the above, What does age and pets represent?

推荐答案

年龄宠物(年龄,宠物)=> new 是传递给匿名方法(或lamba)的两个参数的参数名。



GroupBy 方法需要两个lambas,第一个标识要分组的键,第二个使用该键(上例中的 age )和属于该组的项目( pets )并允许调用者对它们执行某些操作。在这种情况下, something 正在创建一个包含四个属性的匿名类型的新实例;

Key ;团体钥匙或宠物的年龄。

计数;这个年龄段的宠物数量。

最低;集团中最年轻的宠物的年龄。

最高;小组中最老的宠物的年龄。



希望这会有所帮助,

Fredrik
age and pets in (age, pets) => new are the parameter names of the two parameters passed to the anonymous method (or lamba).

The GroupBy method takes two lambas, the first one identifies the key to group on, the second one takes that key (age in the example above) and the items that fall within that group (pets) and allow the caller to do something with them. In this case that "something" is creating a new instance of an anonymous type containing four properties;
Key; The group key, or age of the pets.
Count; The number of pets of this age.
Min; The age of the youngest pet in the group.
Max; The age of the oldest pet in the group.

Hope this helps,
Fredrik


这篇关于Linq的匿名声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆