簡體   English   中英

使用LINQ從文本文件中獲取唯一記錄

[英]Take unique records from text file using LINQ

我已經建立了ICollection<User>列表:

public ICollection<User> MyUsers { get; set; }
public IList<User> GetUserList(string path)
{
        MyUsers = File.ReadAllLines(path)
           .Where(linia => linia.Length > 1)
           .Select(line => Parse(line))
           .ToList();            

        return new List<User>(MyUsers);
}

private static User Parse(string line)
{
        var column = line.Split('|');

        return new User
        {
            ReadTime = column [0],
            idUser = column [1],
            LastName = column [2],
            FirstName = column [3],
            City = column[4]
        };
}

我的源文本文件如下所示:

2019-03-03|1|LN1|FN1|Berlin
2019-03-03|2|LN2|FN2|Rome
2019-03-03|3|LN3|FN3|Wien
2019-03-03|4|LN4|FN4|Londyn
....
2019-03-27|1|LN1|FN1|Berlin
2019-03-27|2|LN2|FN2|Rome
2019-03-27|3|LN3|FN3|Wien
2019-03-27|4|LN4|FN4|Londyn

當我運行此命令時,將獲得具有相同記錄的列表,只有ReadTime是不同的。

如何設置唯一的 MyUsers列表,其中“ ReadTime列將在上次獲取日期?

您可以嘗試使用簡單的GroupBy方法:

  MyUsers = File.ReadAllLines(path)
    .Where(linia => linia.Length > 1)
    .Select(line => Parse(line))
    .GroupBy(
      u => u.idUser, 
      (key, grp) => new User() {
        ReadTime = grp.Select(u => u.ReadTime).Max(),
        idUser = key,
        LastName = grp.Select(u => u.LastName).FirstOrDefault(),
        FirstName = grp.Select(u => u.FirstName).FirstOrDefault(),
        City = grp.Select(u => u.City).FirstOrDefault(),
      })
    .ToList();

您可以使用MoreLINQ -NuGet-package,有用的DistinctBy -function:

MyUsers = File.ReadAllLines(path)
    .Where(linia => linia.Length > 1)
    .Select(line => Parse(line))
    .OrderByDescending(r => r.ReadTime)
    .DistinctBy(r => new { r.City, r.FirstName, r.idUser, r.LastName })
    .ToList();

我們可以GroupBy並找到Max日期為每個組:

   IEnumerable<string> result = File
     .ReadLines(path)    
     .Where(line => !string.IsNullOrWhiteSpace(line)) // to be on the safe side
     .Select(line => {
        int p = line.IndexOf('|');

        return new {
          date = line.Substring(0, p), // date to take Max
          key = line.Substring(p + 1)  // group key
        };
      })
     .GroupBy(item => item.key, item => item.date)
     .Select(chunk => string.Join("|", chunk.Key, chunk.Max(item => item)));

過濾掉重復項后,我們可以將其解析為一個集合:

   MyUsers = result
     .Select(line => Parse(line))
     .ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM