用LINQ得到不同的行



我目前为每个"FileStreamID"有多个记录,但我只想根据最新的"请求日期"获得一个不同的记录

下面是我现在的文件:

 var resultx = from v in ctx.vEmailSents
                             where v.Lab_ID == 44
                                 && v.UploadDate >= this.BeginDate
                             select new
                             {
                                 FileStreamID = v.FileStream_ID,
                                 UploadDate = v.UploadDate,
                                 RequestDate = v.DateSent,
                                 TypeDesc = v.TypeDesc
                             };

现在我得到了大约33条记录(但我应该只得到12条)

我怎么能改变它,只给我不同的行文件流id ?

将您的vEmailSent对象按FileStream_ID分组,并从每个组项中选择最新日期:

 var resultx = from v in ctx.vEmailSents
               where v.Lab_ID == 44
                     && v.UploadDate >= this.BeginDate
               group v by v.FileStream_ID into g
               select g.OrderByDescending(x => x.DateSent)
                       .FirstOrDefault() into lastV
               select new {
                      FileStreamID = lastV.FileStream_ID,
                      UploadDate = lastV.UploadDate,
                      RequestDate = lastV.DateSent,
                      TypeDesc = lastV.TypeDesc                          
               };
var results = resultx
    .GroupBy(x => x.FileStreamID)
    .Select(g => g.OrderByDescending(x => x.RequestDate).First());

相关内容

  • 没有找到相关文章