如何在列表中查找重复日期并对说明进行分组

时间:2014-11-18 20:35:57

标签: c# linq

我有一个这样的对象:

public DateLookupItem()
{
    Id = int.MinValue;
    Date = DateTime.MinValue;
    Description = string.Empty;
}

我有一个上面的对象列表。现在我需要找到一种方法来按相同的日期进行分组并连接该组的描述。

如果列表中的2个日期相同而另一个则不同,我需要输出类似这样的字符串:

(Description/Description) Date, (Description) Date

我有以下代码可以使用,但很难阅读/遵循。 dateListDateLookupItem列表:

string comma = string.Empty;

List<IGrouping<DateTime, DateLookupItem>> sameDates = dateList.GroupBy(x => x.Date).ToList();

foreach(IGrouping<DateTime, DateLookupItem> date in sameDates )
{
    string forwardSlash = string.Empty;
    displayDate += comma + "(";

    foreach(DateLookupItem d in date)
    {
        displayDate += forwardSlash + d.Description;
        forwardSlash = "/";
    }

    displayDate += ")" + " " + CELPDateTime.GetStringFromDateTime(date.Key);
    comma = ", ";
}

有没有办法将其转换为linq?或者重构它以便更容易阅读/理解?

3 个答案:

答案 0 :(得分:3)

这应该这样做:

var sameDates = from d in dateList
        group d.Description by d.Date.Date;

var displayDate = 
    String.Join(",", 
                sameDates
                .Select(date=>string.Format("({0}) {1}", 
                                   String.Join("/", date), 
                                    date.Key)
                       )
                );

您需要将date.Key替换为CELPDateTime.GetStringFromDateTime(date.Key)

这是我使用的其余代码:

void Main()
{
    var dateList = new List<DateLookupItem>();
    for(int i=0; i< 100; ++i) dateList.Add(new UserQuery.DateLookupItem());

    var sameDates = from d in dateList
            group d.Description by d.Date.Date;

    var displayDate = 
        String.Join(",", 
                    sameDates
                    .Select(date=>string.Format("({0}) {1}", String.Join("/", date), date.Key)));


    displayDate.Dump();
}


public class DateLookupItem
{
    static readonly Random rnd = new Random();
        public DateLookupItem()
        {
            Id = rnd.Next();
            Date = DateTime.Now.AddDays(rnd.Next(-5, 5) );
            Description = new string((char) rnd.Next(65, 91), rnd.Next(10)+1);
        }
    public int Id {get; set;}
    public DateTime Date {get; set;}
    public string Description {get; set;}
}

答案 1 :(得分:2)

var data = new List<DateLookupItem>();

var result = from d in data
    group d by d.Date
    into g
    select new
    {
        g.Key,
        descs = g.Select(gg => gg.Description)
    };

var output = string.Join(",", result
    .Select(r => string.Format("({0}) {1}", string.Join("/", r.descs), 
        CELPDateTime.GetStringFromDateTime(r.Key))));

答案 2 :(得分:1)

这是一种单行方式:

var dliList = new List<DateLookupItem>();

// Create a list of ten items. Every third one has the same date.
for (int i = 1; i < 11; i++)
{
    dliList.Add(new DateLookupItem
        {
            Id = i,
            Date = DateTime.Parse(string.Format("1-{0}-2014", 
                (i % 3 == 0) ? "3" : i.ToString())),
            Description = i.ToString(),
        });
}

Console.WriteLine(
    string.Join(", ",
                dliList.GroupBy(x => x.Date)
                        .Select(
                            dliGrp =>
                                string.Format("({0}), {1}", 
                                    string.Join("/", dliGrp.Select(d => d.Description)),
                                    dliGrp.Key))));