LINQ 中的分组依据
假设我们有一个这样的类:
class Person {
internal int PersonID;
internal string car;
}
我有一个此类的列表: List
并且此列表可以有多个具有相同 PersonID
的实例,例如:
persons[0] = new Person { PersonID = 1, car = "Ferrari" };
persons[1] = new Person { PersonID = 1, car = "BMW" };
persons[2] = new Person { PersonID = 2, car = "Audi" };
有没有一种方法可以按 PersonID
进行分组并获取所有的列表他有哪些车?
例如,预期结果是
class Result {
int PersonID;
List<string> cars;
}
所以分组后,我会得到:
results[0].PersonID = 1;
List<string> cars = results[0].cars;
result[1].PersonID = 2;
List<string> cars = result[1].cars;
从我到目前为止所做的事情来看:
var results = from p in persons
group p by p.PersonID into g
select new { PersonID = g.Key, // this is where I am not sure what to do
有人可以指出我正确的方向吗?
Let's suppose if we have a class like:
class Person {
internal int PersonID;
internal string car;
}
I have a list of this class: List<Person> persons;
And this list can have multiple instances with same PersonID
s, for example:
persons[0] = new Person { PersonID = 1, car = "Ferrari" };
persons[1] = new Person { PersonID = 1, car = "BMW" };
persons[2] = new Person { PersonID = 2, car = "Audi" };
Is there a way I can group by PersonID
and get the list of all the cars he has?
For example, the expected result would be
class Result {
int PersonID;
List<string> cars;
}
So after grouping, I would get:
results[0].PersonID = 1;
List<string> cars = results[0].cars;
result[1].PersonID = 2;
List<string> cars = result[1].cars;
From what I have done so far:
var results = from p in persons
group p by p.PersonID into g
select new { PersonID = g.Key, // this is where I am not sure what to do
Could someone please point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
绝对 - 你基本上想要:
或者作为非查询表达式:
基本上,组的内容(当被视为
IEnumerable
时)是投影中任何值的序列(p.car
在这种情况下)存在给定的密钥。有关
GroupBy
工作原理的更多信息,请参阅我的 有关该主题的 Edulinq 帖子。(我已将上面的
PersonID
重命名为PersonId
,以遵循 .NET 命名约定,在“大写复合词和常用术语”部分中专门指出了这一点。)或者,您可以使用
Lookup
:然后您可以非常轻松地获取每个人的汽车:
Absolutely - you basically want:
Or as a non-query expression:
Basically the contents of the group (when viewed as an
IEnumerable<T>
) is a sequence of whatever values were in the projection (p.car
in this case) present for the given key.For more on how
GroupBy
works, see my Edulinq post on the topic.(I've renamed
PersonID
toPersonId
in the above, to follow .NET naming conventions, which specifically call this out in the "Capitalizing Compound Words and Common Terms" section.)Alternatively, you could use a
Lookup
:You can then get the cars for each person very easily:
你也可以试试这个:
You can also Try this:
尝试
或
检查是否有人在您的列表中重复尝试
try
or
to check if any person is repeating in your list try
我使用查询语法和方法语法创建了一个工作代码示例。我希望它对其他人有帮助:)
您还可以在此处的 .Net Fiddle 上运行代码:
这是结果:
I have created a working code sample with Query Syntax and Method Syntax. I hope it helps the others :)
You can also run the code on .Net Fiddle here:
Here is the result:
首先,设置您的关键字段。然后包括您的其他字段:
First, set your key field. Then include your other fields:
试试这个:
但是从性能角度来看,以下实践在内存使用方面更好并且更优化(当我们的数组包含更多项目(例如数百万)时):
Try this :
But performance-wise the following practice is better and more optimized in memory usage (when our array contains much more items like millions):
以下示例使用 GroupBy 方法返回按
PersonID
分组的对象。或者
或者
或者你可以使用
ToLookup
,基本上ToLookup
使用EqualityComparer
。默认比较键并执行使用时应该手动执行的操作分组依据和字典。我认为它是在内存中执行的
The following example uses the GroupBy method to return objects that are grouped by
PersonID
.Or
Or
Or you can use
ToLookup
, BasicallyToLookup
usesEqualityComparer<TKey>
.Default to compare keys and do what you should do manually when using group by and to dictionary.i think it's excuted inmemory
另一种方法是选择不同的
PersonId
并使用persons
进行分组:或者使用流畅的 API 语法:
GroupJoin 生成第一个列表中的条目列表(列表在我们的例子中是
PersonId
),每个列表在第二个列表(persons
列表)中都有一组连接的条目。An alternative way to do this could be select distinct
PersonId
and group join withpersons
:Or the same with fluent API syntax:
GroupJoin produces a list of entries in the first list ( list of
PersonId
in our case), each with a group of joined entries in the second list (list ofpersons
).