【问题标题】:LINQ OrderBy and StringBuilderLINQ OrderBy 和 StringBuilder
【发布时间】:2018-09-03 07:17:23
【问题描述】:

我有以下自定义类

public class Album
{
    public string PhotoName { get; set; }
    public string Location { get; set; }
    public DateTime DateTime { get; set; }
}

我有一个按位置排序然后按日期时间排序的那种类型的列表

List<Album> SortedList = list
  .OrderBy(o => o.Location)
  .ThenBy(o => o.DateTime)
  .ToList();

现在我需要一个 StringBuilder,它将索引号附加到位置旁边,但索引号应该取决于位置,而不是列表

例如,我的列表包含:

> a | London | 12:00:00 AM
> b | London | 1:00:00 AM
> c | Warsaw | 1:30:00 AM
> d | Warsaw | 1:45:00 AM

我的 StringBuilder 必须是:

> London01
> London02
> Warsaw01
> Warsaw02

我尝试过使用它,但它返回列表的索引号

var query = SortedList.IndexOf(list.SingleOrDefault(i => i.DateTime == a.DateTime));

只是想知道我怎样才能达到我需要的要求?

更新:这是我应该放置字符串生成器的部分的完整代码:

public static string Solution(string S)
    {
        string[] group = S.Split("\r\n");
        List<Album> list = new List<Album>();
        StringBuilder sb = new StringBuilder();

        foreach (string g in group)
        {
            string[] album = g.Split(',');
            Album a = new Album();
            a.PhotoName = album[0];
            a.Location = album[1];
            a.DateTime = DateTime.Parse(album[2]);
            list.Add(a);
        }

        List<Album> SortedList = list.OrderBy(o => o.Location).ThenBy(o => o.DateTime).ToList();

        foreach (string g in group)
        {
            string[] album = g.Split(',');
            Album a = new Album();
            a.PhotoName = album[0];
            string[] photodetails = a.PhotoName.Split('.');
            a.Location = album[1];
            a.DateTime = DateTime.Parse(album[2]);

  //this is the part where I must figure out how to build the string

           // var query = SortedList.IndexOf(list.SingleOrDefault(i => i.DateTime == a.DateTime));

            sb.AppendLine(a.Location + query + "." + photodetails[1]);

        }

        string res = sb.ToString();
        return res;
    }

字符串 S 是这样的形式:

@"photo.jpg, Warsaw, 2013-09-05 14:08:15
john.png, London, 2015-06-20 15:13:22
myFriends.png, Warsaw, 2013-09-05 14:07:13
Eiffel.jpg, Paris, 2015-07-23 08:03:02
pisatower.jpg, Paris, 2015-07-22 23:59:59
BOB.jpg, London, 2015-08-05 00:02:03"

并且生成的 StringBuilder 必须是

Warsaw01
London01
Warsaw02
Paris01
Paris02
London02

【问题讨论】:

    标签: linq stringbuilder


    【解决方案1】:

    您可以结合使用GroupBySelect 函数,该函数还包括索引。比SelectMany 更平。

    var result = list
                  .OrderBy(o => o.Location)
                  .ThenBy(o => o.DateTime)
                  .GroupBy(o => o.Location)
                  .SelectMany(o => o.Select((x,i) => x.Location + (i + 1).ToString("d2")));
    

    如果你想把它变成一个字符串,就不需要 StringBuilder,只需这样做:

    var resultString = string.Join("\n", result);
    

    【讨论】:

    • 我已经编辑了我的问题以包含我的完整代码。只是不知道放在哪里。
    • 我再次更新了我的问题。基本上,字符串 S 是用户输入的,结果字符串必须依赖于他的输入。
    • 我回答了你原来的问题,但不明白你现在想要什么。
    【解决方案2】:

    您可以结合使用GroupByAggregate 扩展方法

    var output = 
        albums.GroupBy(album => album.Location)
              .OrderBy(group => group.Key)
              .SelectMany(group => group.OrderBy(album => album.DateTime)
                                        .Select((album, i) => $"{album.PhotoName}{(i+1):00}"))
              .Aggregate(new StringBuilder(),
                         (builder, item) => builder.AppendLine(item))
              .ToString();
    

    【讨论】:

    • 我已经编辑了我的问题以包含我的完整代码。只是不知道该放在哪里。
    猜你喜欢
    • 2012-07-25
    • 2020-06-10
    • 1970-01-01
    • 2014-06-02
    • 2020-09-04
    • 2011-05-29
    • 1970-01-01
    • 2017-06-12
    相关资源
    最近更新 更多