C# 选择";“定制独特”;使用LINQ从列表中删除项目
我有一个策略对象的通用列表 该列表包含以下数据C# 选择";“定制独特”;使用LINQ从列表中删除项目,c#,linq,C#,Linq,我有一个策略对象的通用列表 该列表包含以下数据 id policyNumber policySequence otherData 1 101 1 aaaa 2 101 2 bbbb 3 101 3 cccc 4 102 1 dddd 5 103 1
id policyNumber policySequence otherData
1 101 1 aaaa
2 101 2 bbbb
3 101 3 cccc
4 102 1 dddd
5 103 1 eeee
6 103 2 ffff
我想为每个policyNumber选择一行,其中包含最高的policySequence,这样我就可以得到以下结果:
id policyNumber policySequence created
3 101 3 cccc
4 102 1 dddd
6 103 2 ffff
下面我有一个使用foreach的解决方案,但我想知道在LINQ中是否有更简单、更干净的方法来实现这一点
class Program
{
static void Main(string[] args)
{
List<Policy> policyList = new List<Policy>
{
new Policy {id = 1, policyNumber = 101, policySequence = 1, otherData = "aaaa"},
new Policy {id = 2, policyNumber = 101, policySequence = 2, otherData = "bbbb"},
new Policy {id = 3, policyNumber = 101, policySequence = 3, otherData = "cccc"},
new Policy {id = 4, policyNumber = 102, policySequence = 1, otherData = "dddd"},
new Policy {id = 5, policyNumber = 103, policySequence = 1, otherData = "eeee"},
new Policy {id = 6, policyNumber = 103, policySequence = 2, otherData = "ffff"}
};
List<Policy> filteredPolicyList = new List<Policy>();
foreach(var policy in policyList)
{
if(!filteredPolicyList.Exists(x => x.policyNumber == policy.policyNumber))
{
filteredPolicyList.Add(policy);
}
else
{
var currentPolicyInFilteredList = filteredPolicyList.Where(x => x.policyNumber == policy.policyNumber).First();
if (policy.policySequence > currentPolicyInFilteredList.policySequence)
{
filteredPolicyList.Remove(currentPolicyInFilteredList);
filteredPolicyList.Add(policy);
}
}
}
}
}
public class Policy
{
public int id;
public int policyNumber;
public int policySequence;
public string otherData;
}
类程序
{
静态void Main(字符串[]参数)
{
列表策略列表=新列表
{
新策略{id=1,policyNumber=101,policySequence=1,otherData=“aaaa”},
新策略{id=2,policyNumber=101,policySequence=2,otherData=“bbbbbb”},
新策略{id=3,policyNumber=101,policySequence=3,otherData=“cccc”},
新策略{id=4,policyNumber=102,policySequence=1,otherData=“dddddd”},
新策略{id=5,policyNumber=103,policySequence=1,otherData=“eeee”},
新策略{id=6,policyNumber=103,policySequence=2,otherData=“ffff”}
};
List filteredpolycylist=新列表();
foreach(保单列表中的var策略)
{
如果(!filteredPolicyList.Exists(x=>x.policyNumber==policy.policyNumber))
{
filteredPolicyList.Add(策略);
}
其他的
{
var currentPolicyInfiledList=filteredPolicyList.Where(x=>x.policyNumber==policy.policyNumber).First();
如果(policy.policySequence>currentPolicyInFilteredList.policySequence)
{
FilteredPolycylist.Remove(CurrentPolicyInfiledList);
filteredPolicyList.Add(策略);
}
}
}
}
}
公共阶级政策
{
公共int id;
公共政策编号;
公共政策序列;
公共字符串数据;
}
如果使用LINQ to对象,可以使用项目的DistinctBy
方法:
var maxPolicies = policyList.OrderByDescending(x => x.PolicySequence)
.DistinctBy(x => x.PolicyNumber);
您可以分组和聚合:
var result = from p in policyList
group p by p.policyNumber into g
select new { Policy = g.Key, Max = g.Max() };
给那些发现这个问题的人的一个提示。如果您想按多个条件分组。GroupBy(p=>new{p.criteria,p.criteria1,p.criteria2,})
var result = from p in policyList
group p by p.policyNumber into g
select new { Policy = g.Key, Max = g.Max() };