我有一个具有以下属性的对象列表
public class Client
{
Id Guid,
FromDate datetime
}
客户列表
List<Clients> clients = new List<Clients>();
此列表中有许多相同的ID具有不同的FromDates。如何获取每个Id的Id和最小FromDate列表?
例如:
Id FromDate
1 2015-01-01
1 2016-01-01
2 2015-02-02
2 2015-12-31
我希望最终得到一个包含以下内容的列表:
Id FromDate
1 2015-01-01
2 2015-02-02
答案 0 :(得分:4)
您可以申请group by
:
var query=clients.GroupBy(c=>c.Id)
.Select(g=>new {Id=g.Key, FromDate=g.Min(c=>c.FromDate)});
答案 1 :(得分:0)
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:url];
RKObjectMapping *userMapping = [RKObjectMapping requestMapping];
NSDictionary *objMapping = @{@"id": @"MyId"};
[userMapping addAttributeMappingsFromDictionary:objMapping];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userMapping objectClass:[MyObject class] rootKeyPath:nil method:RKRequestMethodPOST];
[objectManager addRequestDescriptor:requestDescriptor];
objMapping = @{@"id": @"MyId"};
[userMapping addAttributeMappingsFromDictionary:objMapping];
requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userMapping objectClass:[AnotherObject class] rootKeyPath:nil method:RKRequestMethodPOST];
[objectManager addRequestDescriptor:requestDescriptor];
答案 2 :(得分:0)
有两种不同的方式
Distinct
您必须实施自己的IEqualityComparer<Client>
(https://msdn.microsoft.com/en-us/library/bb338049.aspx)
而且你可以致电:
IEnumerable<Client> noduplicates = products.Distinct(new ClientComparer());
GroupBy
var result = clients
.GroupBy(c => c.Id)
.Select(g => new { Id = g.Key, FromDate = g.Min(c => c.FromDate) });
var result = from c in clients
group c by new { id = c.Id } into g
select new { id = g.Key, FromDate = g.Min(c => c.FromDate) };