C#Linq使用与工会不同

时间:2018-07-19 18:51:57

标签: c# linq

我在db中有一个表:

Id Name   Stream  Version   UId  Tab    Key    Value  CreatedOn   CreatedBy
1  Name1   GOP      1       U1   Tab1   co      1     07/01/2018  S, Adam
2  Name1   GOP      1       U2   Tab1   co      1     07/03/2018  S, Adam
3  Name1   GOP      1       U3   Tab2   st      2     07/03/2018  S, Adam
4  Name1   GOP      2       OR   Tab1   co      1     07/02/2018  P, Silver
5  Name2   GOP      1       OR   Tab1   co      1     07/02/2018  P, Silver
6  Name3   GOP      0       OR1  Tab0   coe     1     07/02/2018  S, Adam
7  Name3   GOP      0       OR2  Tab1   coe     1     07/02/2018  S, Adam
8  Name2   LNT      3       NE   Tab1   st      4     07/01/2018  P, Silver
9  Name2   LNT      3       NE1  Tab1   co      2     07/01/2018  P, Silver
10 Name2   LNT      2       NE2  Tab1   st      3     07/01/2018  P, Silver
11 Name2   LNT      0       NE   Tab9   co      5     07/01/2018  R, Henry
12 Name3   TTE      0       TT   Tab1   ee      2     07/02/2018  R. Henry
13 Name3   TTE      0       T1   Tab1   ee      2     07/02/2018  R. Henry

我想编写一个查询,该查询将为我提供set的最高版本以及不同的版本0。

为此,我将查询写为,但这没有得到我想要的输出:

var data = response.GroupBy(x => new { x.Name, x.Stream })
           .SelectMany(g => g.OrderByDescending(row => row.Version).Take(1)) //This gives highest version 
           .Union(response.Where(x => int.Parse(x.Version) == 0)) // This gives version 0
           .OrderByDescending(o => o.CreatedOn).ToList();

用户界面上所需的输出

Id Name   Stream  Version   CreatedOn   CreatedBy                         
4  Name1   GOP      2       07/02/2018  P, Silver   //This is shown as 2 is the highest version for Name1 & GOP combination
5  Name2   GOP      1       07/02/2018  P, Silver   //This is shown as Name & Stream combination is different 
6  Name1   GOP      0       07/02/2018  S, Adam     //Version 0 is always shown - Combination of Name & Stream may or may not have more than one 0 version
8  Name2   LNT      3       07/01/2018  P, Silver   //This is shown as 3 is the highest version for Name2 & LNT combination
11 Name2   LNT      0       07/01/2018  R, Henry    //Version 0 is always shown 
12 Name3   TTE      0       07/02/2018  R, Henry    //Version 0 is always shown

在UI上,我试图仅显示一组的缩小版本。当用户单击该集合时,我将显示该集合中所有单个集合的详细信息。

现在,我只想着如何更新查询,这样我才能得到想要的结果。

----已更新---

现在我正在工作的是个人名单:

 var data1 = response.GroupBy(x => new { x.Name, x.Stream})
              .SelectMany(g => g.OrderByDescending(row => row.Version).Take(1))
              .Where(x => int.Parse(x.Version) != 0)
              .OrderByDescending(o => o.CreatedOn).ToList();

以上提供了给定名称和信息流的所有最新版本。

var data2 = response.GroupBy(x => new { x.Name, x.Stream})
             .Select(g => g.First())
             .Where(x => int.Parse(x.Version) == 0)
             .OrderByDescending(o => o.CreatedOn).ToList();

上面给出了给定名称和流的所有0个版本。

我认为这些单个列表目前可以正常运行,但是如何合并它们。

有没有一种方法可以将这些列表合并/合并在一起,以便仅返回一个集合。或者,如果有一种方法可以将这两个linq查询合并在一起。

---已更新-----

    var set1 = response.GroupBy(x => new { x.Name, x.Stream})
                .SelectMany(g => g.OrderByDescending(row => row.Version).Take(1))
               .Where(x => int.Parse(x.Version) != 0).ToList();

    var set2 = response.GroupBy(x => new { x.Name, x.Stream, x.Version})
                       .Select(g => g.First())
                       .Where(x => int.Parse(x.Version) == 0).ToList();

    var setmerged = set1.Union(set2).OrderByDescending(o => o.CreatedOn).ToList();

通过上面的方法不确定它是否是干净的解决方案。

2 个答案:

答案 0 :(得分:0)

错误使用DISTINCT

这没有道理。 DISTINCT用于过滤重复的行,它们不是唯一的,因为版本号是相同的。 DBS应该从哪里知道应该使用哪个CreatedOn值?

更好的解决方案

您要使用GROUP BY以便将所有具有相同版本值的内容分组。请注意,您需要在其他列上使用MAX()之类的聚合函数才能正确使用它。

答案 1 :(得分:0)

因此,从更新后的问题的外观来看,您想要每个不同版本,名称和流中的最高版本。

根据您提供的数据集和所需的结果集,无需对0版本进行任何特殊处理,因为获取不同版本的条件也应包括0版本。

您还没有真正指定版本的顺序,所以现在我假设最近更新的版本就是您想要的版本

response.GroupBy(x => new { x.Name, x.Stream, x.Version })
        .SelectMany(g => g.OrderByDescending(row => row.CreatedOn).Take(1))
        .ToList();