LINQ从每条记录中获取两条记录

时间:2013-07-25 11:14:15

标签: linq

我有这种类型的对象列表:

MyObject
  .StartDate
  .EndDate
  .Number

对于每个对象,我想返回两个对象:

NewObject
  .Date
  .Number

在一个对象中,Date将是EndDate和Number。 另一个对象是Date将是StartDate,Number将是负数

3 个答案:

答案 0 :(得分:3)

MyObjects.SelectMany(o => new[] { 
    new NewObject { Date = o.EndDate, Number = o.Number },
    new NewObject { Date = o.StartDate, Number = -o.Number } } );

答案 1 :(得分:0)

var result = (from myObject in MyObjects
              select new NewObject { Date = myObject.StartDate, Number = -myObject.number }).Concat
             (from myObject in MyObjects
              select new NewObject { Date = myObject.EndDate, Number = myObject.number });

答案 2 :(得分:0)

var q = from o in MyObjects
        select new {
            NewObject1 = new NewObject(){
                Date = o.StartDate,
                Number = -o.Number
            },
            NewObject2 = new NewObject(){
                Date = o.EndDate,
                Number = o.Number
            }
        };