查询值大于0的集合,同时保持当前月份值是0还是大于0

时间:2015-04-02 18:57:14

标签: c# linq

我有这种情况,我需要选择Bar对象Val大于0,同时保持对象的当前月份Val是0还是大于0。 / p>

var currentMonth = new DateTime(2015, 3, 1); // Just an example
var foo = new List<Bar>()
{
  new Bar { Date = '01/01/2015', Val = 40 },
  new Bar { Date = '02/01/2025', Val = 30 },
  new Bar { Date = '03/01/2015', Val = 0 },
  new Bar { Date = '04/01/2015', Val = 2 },
  new Bar { Date = '05/01/2015', Val = 5 }
}

// I also need to select the current month whether it's Val is 0 or greater than 0
// Stuck here
var fooResult = foo.Where(f => f.Val > 0);

所以结果应该是这样的:

{
  new Bar = { Date = '03/01/2015', Val = 0 },
  new Bar = { Date = '04/01/2015', Val = 2 },
  new Bar = { Date = '05/01/2015', Val = 5 }
}

或者,如果将currentMonth声明为此。 var currentMonth = new DateTime(2015, 4, 1);

结果应该是这样的:

{
  new Bar { Date = '04/01/2015', Val = 2 },
  new Bar { Date = '05/01/2015', Val = 5 }
}

非常感谢任何帮助。感谢

1 个答案:

答案 0 :(得分:2)

试试这个

var fooResult = foo.Where(f => f.Val > 0 || currentMonth.Month ==Convert.ToDateTime(f.Date).Month);
相关问题