Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GroupBy记录考虑不同列上的最高值_C#_.net_Linq_C# 4.0 - Fatal编程技术网

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
我必须过滤IsProductMissingtrueIsProductLogicProcessedfalse的记录,仅针对最高SequenceNumber记录上的CountryIDStateID组合

由于IsProductMissingtrue并且IsProductLogicProcessedfalse对于CountryIDStateID组合的最高序列号,因此将过滤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());
其他查询中有两个问题

  • 不正确的记录集:我将错误地获得输出
  • 因为对于(CountryID,StateID)(1,1)的组合,第二个序列 IsProductMissing=1, IsProductLogicProcessed=0它也错误地选择了ID号3

  • 在编号为2的linq查询中,输出窗口显示它正在通过将ProductDetails表中的所有记录放入内存进行连接,这是非常低效的 如果我有一百万张唱片,它最终会超时的


    那么,如何简化此过程并使用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());