修改Linq以返回第一个匹配值

本文关键字:第一个 返回 Linq 修改 | 更新日期: 2023-09-27 18:11:54

我试图修改下面的Linq查询,以返回最大版本的第一个匹配行。

+----+------+---------+-----+
| PK | Name | Version | abc |
+----+------+---------+-----+
|  1 | Bill |       1 |   7 |
|  2 | Bob  |       1 |   2 |
|  3 | Ben  |       1 |   3 |
|  4 | Bob  |       2 |   2 |
|  5 | Ben  |       2 |   3 |
|  6 | Ben  |       2 |   3 |
+----+------+---------+-----+

当前会返回:

 +----+------+---------+-----+
    | PK | Name | Version | abc |
    +----+------+---------+-----+
    |  1 | Bill |       1 |   7 |
    |  4 | Bob  |       2 |   2 |
    |  5 | Ben  |       2 |   3 |
    |  6 | Ben  |       2 |   3 |
    +----+------+---------+-----+

当我想让它返回时:

+----+------+---------+-----+
| PK | Name | Version | abc |
+----+------+---------+-----+
|  1 | Bill |       1 |   7 |
|  4 | Bob  |       2 |   2 |
|  5 | Ben  |       2 |   3 |
+----+------+---------+-----+
 var source = (from item in baseSource
                          where item.Version > 0 
                          where item.Published                           
                          where db.abcTest.Where(it => it.Published && it.Name == item.Name).Max(it => it.Version) == item.Version                              
                          orderby item.Name, item.Version descending                             
                          select new
                          {                         
                              item.PK,
                              item.Name,                                 
                              item.Version,
                              item.abc
                          }
                          ).ToList();
            return source;

修改Linq以返回第一个匹配值

这里需要GroupBy:-

var result = baseSource.GroupBy(x => x.Name)
                .Select(x =>
                {
                    var orderedObj = x.OrderByDescending(z => z.Version)
                                      .ThenByDescending(z => z.abc).FirstOrDefault();
                    return new 
                            {
                                 PK = orderedObj.PK,
                                 Name = x.Key,
                                 Version = orderedObj.Version ,
                                 abc = orderedObj.abc
                             };
                };

考虑检查orderedObj是否为空,否则可能抛出Null引用异常。

下面的代码使用group函数使其工作

var source = (from item in baseSource
                          where item.Version > 0 
                          where item.Published                           
                          where db.abcTest.Where(it => it.Published && it.Name == item.Name).Max(it => it.Version) == item.Version
                          orderby item.Name, item.Version descending
                          group item by new { item.Name, item.Version} into groupx
                          select new
                          {          
                                PK = groupx.FirstOrDefault().PK,
                                groupx.Key.Name,
                                groupx.Key.Version,
                                abc= groupx.Select(it => it.abc.Count()).FirstOrDefault()
                          }
                          ).ToList();
            return source;