c#在linq语句中获取最后一个元素[英] c# getting last element in LINQ statement

本文是小编为大家收集整理的关于c#在linq语句中获取最后一个元素的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

    var filePaths = Directory.GetFiles(@"\\Pontos\completed\", "*_*.csv").Select(p => new { Path = p, Date = System.IO.File.GetLastWriteTime(p) })
        .OrderBy(x => x.Date)
        .Where(x => x.Date >= LastCreatedDate);

我想知道最新的x.Date

的价值

从这个linq语句中我如何获得最近的日期?

请注意,我不需要filepath而不是我需要DATE

推荐答案

var mostRecent = Directory.GetFiles(@"\\Pontos\completed\", "*_*.csv")
    .Select(p => new { Path = p, Date = System.IO.File.GetLastWriteTime(p) }) 
    .OrderBy(x => x.Date) 
    .Where(x => x.Date >= LastCreatedDate)
    .LastOrDefault(); 

var mostRecent = Directory.GetFiles(@"\\Pontos\completed\", "*_*.csv")
    .Select(p => new { Path = p, Date = System.IO.File.GetLastWriteTime(p) }) 
    .OrderByDescending(x => x.Date) 
    .Where(x => x.Date >= LastCreatedDate)
    .FirstOrDefault(); 

其他推荐答案

只需撤消订单 - 还要在订购之前进行过滤:

 var filePaths = Directory.GetFiles(@"\\Pontos\completed\", "*_*.csv").Select(p => new { Path = p, Date = System.IO.File.GetLastWriteTime(p) })
        .Where(x => x.Date >= LastCreatedDate)
        .OrderByDescending(x => x.Date)
        .FirstOrDefault();

相反,我建议您使用DirectoryInfo's GetFiles(),而是返回FileInfo实例,这样您就不必手动抓住最后的写作时间:

var di = new DirectoryInfo(@"\\Pontos\completed\");
var file =  di.GetFiles("*_*.csv")
              .Where(f=> f.LastWriteTimeUtc >= LastCreatedDate)
              .OrderByDescending(f => f.LastWriteTimeUtc)
              .FirstOrDefault();


if(file!=null)
{
   Console.WriteLine("Path: {0}, Last Write Time: {1}", file.FullName, 
                                                        file.LastWriteTimeUtc);
}

其他推荐答案

f#具有我喜欢使用的方便MaxBy()函数; C#实施是微不足道的.它允许您避免对序列进行排序的成本.

有关更多详细信息,请参见此答案: https://stackoverflow.com/a/a/a/8759648/385844

用法:

var mostRecent = Directory.GetFiles(@"\\Pontos\completed\", "*_*.csv")  
    .Select(p => new { Path = p, Date = System.IO.File.GetLastWriteTime(p) })   
    .Where(x => x.Date >= LastCreatedDate)  
    .MaxBy(x => x.Date);

本文地址:https://www.itbaoku.cn/post/1557017.html

问题描述

    var filePaths = Directory.GetFiles(@"\\Pontos\completed\", "*_*.csv").Select(p => new { Path = p, Date = System.IO.File.GetLastWriteTime(p) })
        .OrderBy(x => x.Date)
        .Where(x => x.Date >= LastCreatedDate);

i would like to know the value of the most recent x.Date

from this linq statement how can i get the most recent date?

please note that i do not need the filepath rather i need the DATE

推荐答案

var mostRecent = Directory.GetFiles(@"\\Pontos\completed\", "*_*.csv")
    .Select(p => new { Path = p, Date = System.IO.File.GetLastWriteTime(p) }) 
    .OrderBy(x => x.Date) 
    .Where(x => x.Date >= LastCreatedDate)
    .LastOrDefault(); 

or

var mostRecent = Directory.GetFiles(@"\\Pontos\completed\", "*_*.csv")
    .Select(p => new { Path = p, Date = System.IO.File.GetLastWriteTime(p) }) 
    .OrderByDescending(x => x.Date) 
    .Where(x => x.Date >= LastCreatedDate)
    .FirstOrDefault(); 

其他推荐答案

Just reverse the order - also do the filtering before the ordering:

 var filePaths = Directory.GetFiles(@"\\Pontos\completed\", "*_*.csv").Select(p => new { Path = p, Date = System.IO.File.GetLastWriteTime(p) })
        .Where(x => x.Date >= LastCreatedDate)
        .OrderByDescending(x => x.Date)
        .FirstOrDefault();

Instead I would suggest you use DirectoryInfo's GetFiles() instead which returns FileInfo instances so you don't have to grab the last write time manually:

var di = new DirectoryInfo(@"\\Pontos\completed\");
var file =  di.GetFiles("*_*.csv")
              .Where(f=> f.LastWriteTimeUtc >= LastCreatedDate)
              .OrderByDescending(f => f.LastWriteTimeUtc)
              .FirstOrDefault();


if(file!=null)
{
   Console.WriteLine("Path: {0}, Last Write Time: {1}", file.FullName, 
                                                        file.LastWriteTimeUtc);
}

其他推荐答案

F# has a handy MaxBy() function that I like to use; the C# implementation is trivial. It allows you to avoid the cost of sorting the sequence.

See this answer for more detail: https://stackoverflow.com/a/8759648/385844

usage:

var mostRecent = Directory.GetFiles(@"\\Pontos\completed\", "*_*.csv")  
    .Select(p => new { Path = p, Date = System.IO.File.GetLastWriteTime(p) })   
    .Where(x => x.Date >= LastCreatedDate)  
    .MaxBy(x => x.Date);