通过字段中选择属性我有一个对象Record
集团使用LINQ
它包含以下属性:
Guid RecordId
Guid UserId
现在我必须做出分组并使用此Linq查询:
Records.GroupBy(m => m.User)
问题是User
对象从2个地方填充,有时数据不匹配的属性,从而产生具有用于单一UserId
例如多个结果:
List<Record>
{
new Record
{
RecordId = 1,
User = new User
{
UserId = 1,
UserName = '1',
OtherProperty = false
}
UserId = 1
},
new Record
{
RecordId = 2,
User = new User
{
UserId = 1,
UserName = '1',
OtherProperty = true
}
UserId = 1
},
new Record
{
RecordId = 3,
User = new User
{
UserId = 2,
UserName = '2',
OtherProperty = false
}
UserId = 2
}
}
的OtherProperty
是在User
对象的不同,导致没有被分组。
我需要一个User
对象的分组,但实际上使用UserId
作为分组字段。
问题是,这是我现在有:'的IEnumerable>',但你的方法,我得到这样的结果:'的IEnumerable >'。我失去了'用户'键。我想'User'作为关键词,但是'UserId'作为groupby选择器。 –
YesMan85
如果是这样的话,那么你需要写一个'User'的相等比较为(伪):'return u1.UserID == u2.UserID;'。在你做完这些之后,你将能够'GroupBy'用户。 –
请问为什么,如果你有'Record'作为值,'Record'包含'User',你想让用户自己成为分组键? –