如何 select 根据条件从 C# 中的列表中复制值?
How to select duplicate value from a list in C# based on condition?
我有一个以日期和 Fileno 作为值的列表。我需要找到重复的日期,并根据该日期找到最高的 Fileno.Then 将该键值对和不同的对添加到最终列表中。结果应该如图所示below.I我可以得到重复的日期但是如何比较重复的日期并找到最高的文件号?
Key Date Fileno
1 10/8/1980 1234
2 10/8/1980 1345
3 8/6/1970 4567
结果
2 10/8/1980 1345
3 8/6/1970 4567
代码
var list = new List<valuepair>();
list.Add(new valuepair {no=key,comdate=date,filnum=fileno})
Var dup= list.GroupBy(x => comdate.Value).Where(x => comdate.Count() > 1)
您快完成了,只是您发布的代码不可编译 (!)。您只需要从每个组中取出具有最高“filnum”的元素:
var list = new List<valuepair>();
list.Add(new valuepair { no=1, comdate="10/8/1980", filnum=1234 });
list.Add(new valuepair { no=2, comdate="10/8/1980", filnum=1345 });
list.Add(new valuepair { no=3, comdate="8/6/1970", filnum=4567 });
var listWithoutDuplicates = list.GroupBy(x => x.comdate)
// For each group (in which items have identical dates) take only
// the one with the highest "filnum"
.Select(group => group.OrderBy(x => x.filnum).First())
.ToList();
试试这个:
var result = data
.GroupBy(
i => i.Date,
(key, group) => group.Single(x => x.Fileno == group.Max(y => y.Fileno)))
.ToList();
我有一个以日期和 Fileno 作为值的列表。我需要找到重复的日期,并根据该日期找到最高的 Fileno.Then 将该键值对和不同的对添加到最终列表中。结果应该如图所示below.I我可以得到重复的日期但是如何比较重复的日期并找到最高的文件号?
Key Date Fileno
1 10/8/1980 1234
2 10/8/1980 1345
3 8/6/1970 4567
结果
2 10/8/1980 1345
3 8/6/1970 4567
代码
var list = new List<valuepair>();
list.Add(new valuepair {no=key,comdate=date,filnum=fileno})
Var dup= list.GroupBy(x => comdate.Value).Where(x => comdate.Count() > 1)
您快完成了,只是您发布的代码不可编译 (!)。您只需要从每个组中取出具有最高“filnum”的元素:
var list = new List<valuepair>();
list.Add(new valuepair { no=1, comdate="10/8/1980", filnum=1234 });
list.Add(new valuepair { no=2, comdate="10/8/1980", filnum=1345 });
list.Add(new valuepair { no=3, comdate="8/6/1970", filnum=4567 });
var listWithoutDuplicates = list.GroupBy(x => x.comdate)
// For each group (in which items have identical dates) take only
// the one with the highest "filnum"
.Select(group => group.OrderBy(x => x.filnum).First())
.ToList();
试试这个:
var result = data
.GroupBy(
i => i.Date,
(key, group) => group.Single(x => x.Fileno == group.Max(y => y.Fileno)))
.ToList();