C# GroupBy记录考虑不同列上的最高值
我试图通过在两个特定列上分组并获取最高序列号来处理一组记录 我的表格结构如下: 产品详细信息C# GroupBy记录考虑不同列上的最高值,c#,.net,linq,c#-4.0,C#,.net,Linq,C# 4.0,我试图通过在两个特定列上分组并获取最高序列号来处理一组记录 我的表格结构如下: 产品详细信息 ID CountryID StateID Sequence IsProductMissing IsProductLogicProcessed 1 1 1 1 0 0 2
ID CountryID StateID Sequence IsProductMissing IsProductLogicProcessed
1 1 1 1 0 0
2 1 1 2 1 0
3 1 1 3 0 0
4 2 2 1 0 0
5 2 2 2 1 0
6 4 6 1 1 0
我的预期产出是:
ID CountryID StateID Sequence IsProductMissing IsProductLogicProcessed
5 2 2 2 1 0
6 4 6 1 1 0
我必须过滤IsProductMissing为true和IsProductLogicProcessed为false的记录,仅针对最高SequenceNumber记录上的CountryID和StateID组合
由于IsProductMissing是true并且IsProductLogicProcessed是false对于CountryID和StateID组合的最高序列号,因此将过滤IDs 5和IDs 6
--
我试过做以下事情
var takeDistinctCountryIDStateID = (await _ProductDetails.GetAll().Where(x=>x.IsProductMissing == true &&
x.IsProductLogicProcessed == false).Select(x=>new {
x.CountryID,
x.StateID
}).Distinct().Take(20)).ToListAsync());
其他查询中有两个问题
那么,如何简化此过程并使用Linq/lambda表达式获得预期的输出呢?//对于x.IsProductMissing==true的候选元素&& x、 IsProductLogicProcessed==false //通过查看StateId和CountryId是否存在更高的序列,检查这是否是StateId和CountryId的最高序列
var yourList = (await _ProductDetails.GetAll());
var takeDistinctCountryIDStateID = (yourList.Where(x => x.IsProductMissing == true &&
x.IsProductLogicProcessed == false && !yourList.Where(y=> y.StateId == x.StateId && y.CountryID == x.CountryID && y.Sequece > x.Sequence).Any()).Select(x => new {
x.CountryID,
x.StateID
}).Distinct().Take(20)).ToListAsync());
对于100万条记录来说,这可能会导致性能低下-您可能希望使用排序列表,并通过Lambda函数使用二进制搜索技术来加速x.IsProductMissing==true的候选元素&& x、 IsProductLogicProcessed==false //通过查看StateId和CountryId是否存在更高的序列,检查这是否是StateId和CountryId的最高序列
var yourList = (await _ProductDetails.GetAll());
var takeDistinctCountryIDStateID = (yourList.Where(x => x.IsProductMissing == true &&
x.IsProductLogicProcessed == false && !yourList.Where(y=> y.StateId == x.StateId && y.CountryID == x.CountryID && y.Sequece > x.Sequence).Any()).Select(x => new {
x.CountryID,
x.StateID
}).Distinct().Take(20)).ToListAsync());
对于100万条记录而言,这可能会导致性能低下-您可能希望使用排序列表,并通过Lambda函数使用二进制搜索技术来加快速度您可以通过向我们提供示例数据吗您可以通过向我们提供示例数据吗
ID CountryID StateID Sequence IsProductMissing IsProductLogicProcessed
3 1 1 3 0 0
5 2 2 2 1 0
6 4 6 1 1 0
var yourList = (await _ProductDetails.GetAll());
var takeDistinctCountryIDStateID = (yourList.Where(x => x.IsProductMissing == true &&
x.IsProductLogicProcessed == false && !yourList.Where(y=> y.StateId == x.StateId && y.CountryID == x.CountryID && y.Sequece > x.Sequence).Any()).Select(x => new {
x.CountryID,
x.StateID
}).Distinct().Take(20)).ToListAsync());