如何在列表中获得更大的一天?

时间:2016-01-07 04:11:04

标签: c#

我有1个列表<>在C#中包含许多日/月/年。我希望在列表中获得最大的一天(基于月份/年份进行比较)。该怎么做?

像:

2/5/2015 6/5/2015 16/8/2015 30/7/2015 //note that is from each time in DTOSaveFromFile

最重要的一天是:16/8/2015

这是我的代码:

List<DTOSaveFromFile> lst = Load();

public static List<DTOSaveFromFile> Load()
{
   string[] data = File.ReadAllLines(dataPath);
   return (from i in data select new DTOSaveFromFile(i)).ToList<DTOSaveFromFile>();
}

foreach (var rows in lst)
{
}    

public class DTOSaveFromFile : List<string>
{
    public string reportName { get; set; }
    public string eMail { get; set; }
    public string time { get; set; }

    public DTOSaveFromFile(string _reportname, string _email, string _time)
    {
        reportName = _reportname;
        eMail = _email;
        time = _time;
    }
}

3 个答案:

答案 0 :(得分:1)

你可以使用linq的Max方法,所以试试这个:

var dateBase = new DateTime(2016,01,01);
var dates = Enumerable.Range(0,5).Select(i=> dateBase.AddDays(i).ToString("dd/MM/yyyy")).ToList();
dates.ForEach(d=>Console.WriteLine(d));
Console.WriteLine();
var bigger = dates.Max(d=>DateTime.ParseExact(d, "dd/MM/yyyy", CultureInfo.InvariantCulture));
Console.WriteLine("Bigger Day: {0}",bigger.Day);
Console.WriteLine("Bigger Month: {0}",bigger.Month);
Console.WriteLine("Bigger Year: {0}",bigger.Year);

看到它在我的小提琴中工作:https://dotnetfiddle.net/gxw4Pm

答案 1 :(得分:1)

最终解决方案

您可以在一行中使用LINQ:

DateTime biggest = lst
    .Select(x => DateTime.ParseExact(x.time, "d/M/yyyy", System.Globalization.CultureInfo.InvariantCulture)) //to get the list of `string` of `time` from your List<DTOSaveFromFile> + //To convert it to DateTime 
    .Max(); //Get the max among the DateTime

<强>解释

添加,因为提问者需要更多澄清。

这就是你实际做的事情:

  1. IEnumerable<DateTime>
  2. 获取List<DTOSaveFromFile>
  3. 对于每个DTOSaveFromFile,获取DTOSaveFromFile.time string(注意x.time),
  4. 使用DateTime 正确 x.time(在这种情况下为{1}},您可以获得每个string DateTime.ParseExact的{​​{1}}值format)。
  5. 将所有d/M/yyyy放入单个IEnumerable后,使用DateTime找到其中的最大值。
  6. 分步指导

    1. 基本上,如果Max() Select(x => x.time) List<DTO>类包含DTO字段time(就像您的情况一样)string,然后,而不是拥有DTO列表,你有一个string的“列表”(或更恰当地称为IEnumerable),这是time字段的类型。因此

      List<string> timeList = lst.Select(x => x.time).ToList();
      

      会为您List<string> timeList提供仅timestring)。

    2. 2-3。接下来,对于string中的每个timeList,您需要处理DateTime以找到其中的最大值。你是怎么做到的?对每个DateTime.ParseExact使用string,并为format使用正确的DateTime.ParseExact参数,即"d/M/yyyy"

      因此,你可能想做这样的事情

      List<DateTime> dtlist = new List<DateTime>();
      foreach (string time in timeList){
          DateTime dt = DateTime.ParseExact(x.time, "d/M/yyyy", System.Globalization.CultureInfo.InvariantCulture);
          dtlist.Add(dt);
      }
      

      这将为您List<DateTime>提供dtlist

      但是在Linq中,您可以执行此操作,无需 foreach循环!

      Select(x => DateTime.ParseExact(x, "d/M/yyyy", System.Globalization.CultureInfo.InvariantCulture))
      
      1. 最后,给定dtlist,您希望找到最大值。您只需使用Max即可获得它。

        DateTime biggest = dtlist.Max();
        
      2. 对LINQ的暴力

        现在,实际的解决方案是将所有这些步骤合并到一个 LINQ行中。

        那你在这里做什么:

        List<string> timeList = lst.Select(x => x.time).ToList(); //Step 1
        
        List<DateTime> dtlist = new List<DateTime>(); //Step 2-3
        foreach (string time in timeList){
            DateTime dt = DateTime.ParseExact(x.time, "d/M/yyyy", System.Globalization.CultureInfo.InvariantCulture);
            dtlist.Add(dt);
        }
        
        DateTime biggest = dtlist.Max(); //Step 4
        

        等于一行LINQ

        DateTime biggest = lst
            .Select(x => DateTime.ParseExact(x.time, "d/M/yyyy", System.Globalization.CultureInfo.InvariantCulture)) //to get the list of `string` of `time` from your List<DTOSaveFromFile> + //To convert it to DateTime 
            .Max(); //Get the max among the DateTime
        

        最终解决方案(再次)

        鉴于您的List<DTOSaveFromFile>

        为了便于解释,在selects中使用两个 LINQ,您可以在一行中执行此操作:

        DateTime biggest = lst.Select(x => x.time) //to get the list of `string` of `time` from your List<DTOSaveFromFile>
            .Select(x => DateTime.ParseExact(x, "d/M/yyyy", System.Globalization.CultureInfo.InvariantCulture)) //To convert it to DateTime
            .Max(); //Get the max among the DateTime
        

        您的值在biggest变量中。

        请注意,现在您可以使用select

        进一步简化答案
        DateTime biggest = lst
            .Select(x => DateTime.ParseExact(x.time, "d/M/yyyy", System.Globalization.CultureInfo.InvariantCulture)) //to get the list of `string` of `time` from your List<DTOSaveFromFile> + //To convert it to DateTime 
            .Max(); //Get the max among the DateTime
        

答案 2 :(得分:1)

您可以在列表中填充日期,然后使用order by,如下所示。

private DateTime GetLargeDate(List<DateTime> dates)
    {
        dates = dates.OrderByDescending(t => t.Date).ToList();
        return dates[0];
    }