我怎样才能在c#中使用linq?

时间:2014-04-03 14:38:40

标签: c# linq

我在c#中有一个计划对象列表,包含以下条目:

编辑:编辑复制/粘贴数据

类:

    public class Trade
    {
        public int Id { get; set; }
        public String StartDate { get; set; }
        public String EndDate { get; set; }
        public Time[] Times { get; set; }
    }

    public class Time
    {
        public int Id { get; set; }
        public int Start { get; set; }
        public int End { get; set; }
    }

阵列:

[TestFixture]
public class LinqTest
{
    [Test]
    public void Test1()
    {
        var trades = new Trade[]
        {
            new Trade
            {
                Id = 1,
                StartDate = "01/01/2014",
                EndDate = "01/01/2014",
                Times = new[] {new Time {Id = 1, Start = 60, End = 120}, new Time {Id = 2, Start = 180, End = 240}}
            },
            new Trade
            {
                Id = 2,
                StartDate = "02/01/2014",
                EndDate = "02/01/2014",
                Times = new[] {new Time {Id = 1, Start = 60, End = 120}, new Time {Id = 2, Start = 180, End = 240}}
            },
            new Trade
            {
                Id = 3,
                StartDate = "03/01/2014",
                EndDate = "03/01/2014",
                Times = new[] {new Time {Id = 1, Start = 60, End = 120}, new Time {Id = 2, Start = 180, End = 240}}
            },
            new Trade
            {
                Id = 4,
                StartDate = "04/01/2014",
                EndDate = "04/01/2014",
                Times = new[] {new Time {Id = 1, Start = 60, End = 120}}
            },
            new Trade
            {
                Id = 5,
                StartDate = "05/01/2014",
                EndDate = "05/01/2014",
                Times = new[] {new Time {Id = 1, Start = 160, End = 220}, new Time {Id = 2, Start = 380, End = 840}}
            },
            new Trade
            {
                Id = 6,
                StartDate = "06/01/2014",
                EndDate = "06/01/2014",
                Times = new[] {new Time {Id = 1, Start = 160, End = 220}, new Time {Id = 2, Start = 380, End = 840}}
            }
        };
    }

我想合并其StartDate顺序且Times条目Start和End相同的条目。所以输出应该是:

[ 
 { 
   Id = 1, 
   StartDate = "01/01/2014", 
   EndDate = "03/01/2014" 
   Times = [{ Id = 1 Start = 60, End = 120 }, { Id = 2 Start = 180, End = 240 }]
 },
 { 
   Id = 4, 
   StartDate = "04/01/2014", 
   EndDate = "04/01/2014" 
   Times = [{ Id = 1 Start = 60, End = 120 }]
 },
 { 
   Id = 5, 
   StartDate = "05/01/2014", 
   EndDate = "06/01/2014" 
   Times = [{ Id = 1 Start = 160, End = 220 }, { Id = 2 Start = 380, End = 840 }]
 },
]

我如何在c#中执行此操作(如果可能,使用linq)?

1 个答案:

答案 0 :(得分:0)

为时间数组使用自定义比较器。:

    public class TimeArrayComparer : IEqualityComparer<Time[]>
    {
        public bool Equals(Time[] x, Time[] y)
        {
            if (x == y) return true;
            if (x == null) return false;
            return GetHashCode(x) == GetHashCode(y);
        }

        public int GetHashCode(Time[] obj)
        {
            if(obj == null) throw new ArgumentNullException("obj");
            return obj.Aggregate(-1, (current, time) => current ^ (time.Start.GetHashCode() ^ time.End.GetHashCode()));
        }
    }

然后GroupBy Times,并选择组中的第一项,将EndDate设置为组中的最后一项:

        var groups = trades.GroupBy(trade => trade.Times, new TimeArrayComparer());

        var result = groups.Select(grouping =>
        {
            var trade = grouping.First();
            trade.EndDate = grouping.Last().EndDate;
            return trade;
        });

但你应该实现一个比这更好的比较器!这只是为了演示!