1

我目前有一个“Day”对象列表。Day 的类定义包含两个字符串: -name -hours

我需要做的是在列表中找到具有相同时间的所有 Day(s) 并将它们格式化为一个项目。

前任。

Sunday 8am - 9pm
Monday 8am - 10pm
Tuesday 8am - 10pm
Wednesday 8am -10pm
Thursday 9am - 10pm
Friday 9am - 11pm
Saturday 8am - 11pm

所以,星期一星期二和星期三都有相同的时间。我需要从原始列表中创建一个新列表,如下所示:

Sunday 8am - 9pm
Mon-Wed 8am -10 pm
...
...

有没有一种简单的方法可以用 LINQ 做到这一点?

4

3 回答 3

1

这接近你想要的:

        IList<Day> days = new List<Day>();
        days.Add(new Day { Name = "Sunday", Hours = "8am - 9pm" });
        days.Add(new Day { Name = "Monday", Hours = "8am - 10pm" });
        days.Add(new Day { Name = "Tuesday", Hours = "8am - 10pm" });
        days.Add(new Day { Name = "Wednesday", Hours = "8am - 10pm" });
        days.Add(new Day { Name = "Thursday", Hours = "9am - 10pm" });
        days.Add(new Day { Name = "Friday", Hours = "9am - 11pm" });
        days.Add(new Day { Name = "Saturday", Hours = "8am - 11pm" });
        var grouped = days.GroupBy(d => d.Hours).ToList();
        StringBuilder sb = new StringBuilder();
        foreach (var group in grouped)
        {
            sb.AppendFormat("{0}: {1}\n", group.Key, group.Select(g => g.Name).Aggregate((list, nextday) => list + ", " + nextday));
        }
        var table = sb.ToString();

输出是:

上午 8 点至晚上 9 点:周日

上午 8 点至晚上 10 点:周一、周二、周三

上午 9 点至晚上 10 点:星期四

上午 9 点至晚上 11 点:周五

上午 8 点至晚上 11 点:周六

于 2013-03-19T23:42:50.743 回答
1

使用GroupConsecutive您将在答案末尾找到的扩展方法,您可以执行以下操作:

List<Day> days = new List<Day>
{
    new Day { Name = "Sunday", Hours= "8am - 9pm" },
    new Day { Name = "Monday", Hours= "8am - 10pm" },
    new Day { Name = "Tuesday", Hours= "8am - 10pm" },
    new Day { Name = "Wednesday", Hours= "8am - 10pm" },
    new Day { Name = "Thursday", Hours= "9am - 10pm" },
    new Day { Name = "Friday", Hours= "8am - 11pm" },
    new Day { Name = "Saturday", Hours= "8am - 11pm" },
};

var query = from g in days.GroupConsequtive(d => d.Hours)
            select new {
                Name = g.Count() == 1 ? g.First().Name :
                       String.Format("{0}-{1}", g.First().Name.Substring(0, 3), g.Last().Name.Substring(0, 3)),
                Hours = g.Key
            };

枚举结果:

foreach (var item in query)            
    Console.WriteLine("{0} {1}", item.Name, item.Hours);

输出:

Sunday 8am - 9pm
Mon-Wed 8am - 10pm
Thursday 9am - 10pm
Fri-Sat 8am - 11pm

连续分组的扩展方法:

public static class LazyExtensions
{
    public static IEnumerable<IGrouping<TKey, TElement>> GroupConsecutive<TKey, TElement>(
        this IEnumerable<TElement> source, Func<TElement, TKey> keySelector)
    {
        if (!source.Any())
            yield break;

        var comparer = Comparer<TKey>.Default;
        Grouping<TKey, TElement> group = null;

        foreach (var item in source)
        {
            var key = keySelector(item);

            if (group == null)
                group = new Grouping<TKey, TElement>(key);

            if (comparer.Compare(group.Key, key) != 0)
            {
                yield return group;
                group = new Grouping<TKey, TElement>(key);
            }

            group.Elements.Add(item);
        }

        yield return group;
    }

    private class Grouping<TKey, TElement> : IGrouping<TKey, TElement>
    {
        public Grouping(TKey key)
        {
            Key = key;
            Elements = new List<TElement>();
        }

        public List<TElement> Elements { get; private set; }
        public TKey Key { get; private set; }

        public IEnumerator<TElement> GetEnumerator()
        {
            return Elements.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}

这个怎么运作?它创建分组(自定义类,实现 IGrouping 接口)并将具有相同键值的连续元素添加到该组。如果新的键值到达,则返回分组。因此,所有序列都按连续的键值分组。

于 2013-03-20T00:43:56.237 回答
0
List<Tuple<string,string>> days = new List<Tuple<string,string>>
(
    new Tuple<string,string>[]
    {
        new Tuple<string,string>( "Sunday", "8am - 9pm" ),
        new Tuple<string,string>( "Monday", "8am - 10pm" ),
        new Tuple<string,string>( "Tuesday", "8am - 10pm" ),
        new Tuple<string,string>( "Wednesday", "8am - 10pm" ),
        new Tuple<string,string>( "Thursday ", "9am - 10pm" ),
        new Tuple<string,string>( "Friday", "9am - 11pm" ),
        new Tuple<string,string>( "Saturday ", "8am - 11pm" )
    }
);

ILookup<string,string> table = days.ToLookup( k => k.Item2, v => v.Item1 );

table[ "8am - 9pm" ]; // Sunday
table[ "8am - 10pm" ]; // Monday, Tuesday, Wednesday 
于 2013-03-19T23:27:33.773 回答