Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.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# 对linq查询结果进行排序,如果它有;第一个()和“分组”;_C#_Asp.net_Linq_Sorting_Sql Order By - Fatal编程技术网

C# 对linq查询结果进行排序,如果它有;第一个()和“分组”;

C# 对linq查询结果进行排序,如果它有;第一个()和“分组”;,c#,asp.net,linq,sorting,sql-order-by,C#,Asp.net,Linq,Sorting,Sql Order By,我想使用日期时间列msbf\u fac\u dt对上述结果进行排序 所以我做了如下改变 var qryLatestInterview = from rows in dt.AsEnumerable() group rows by new { PositionID = r

我想使用日期时间列
msbf\u fac\u dt
对上述结果进行排序

所以我做了如下改变

var qryLatestInterview = from rows in dt.AsEnumerable()                                     
                         group rows by new 
                         { 
                           PositionID = rows["msbf_acc_cd"], 
                           CandidateID = rows["msbf_fac_tp"] 
                         } into grp
                         select grp.First();

但是这不是按上面的
msbf\u fac\u dt
列排序,我在这里可以做什么您可以在选择第一条记录之前对组进行排序:

var qryLatestInterview = from rows in dt.AsEnumerable()
                         orderby rows["msbf_fac_dt"] ascending
                         group rows by new 
                         {
                           PositionID = rows["msbf_acc_cd"], 
                           CandidateID = rows["msbf_fac_tp"], 
                           FacilityDate = rows["msbf_fac_dt"] 
                         } into grp
                         select grp.First();

如果我理解正确,您想从这个列表中获取最近的便利记录,您可以在分组后进行排序并获取第一条记录

var qryLatestInterview = from rows in dt.AsEnumerable()
                         group rows by new
                         {
                             PositionID = rows["msbf_acc_cd"],
                             CandidateID = rows["msbf_fac_tp"],
                         } into grp
                         select grp.OrderBy(x=> x["msbf_fac_dt"]).First();

也尝试了该方法,获取以下信息:System.Data.DataRow不包含“msbf_fac_dt”的定义,并且找不到接受类型为“System.Data.DataRow”的第一个参数的扩展方法“msbf_fac_dt”抱歉,在编写注释时已编辑了我的答案。
dt.AsEnumerable()
  .GroupBy(row => new 
   { 
         PositionID = rows["msbf_acc_cd"], 
         CandidateID = rows["msbf_fac_tp"]
   })
  .Select(x=>x.OrderByDescending(o=>o["msbf_fac_dt"]>).FirstOrDefault() 
  .ToList();