-1

我有以下日期列表:

List<Datetime> listOfDates = new List<DateTIme>();

listofDates.add(new DateTime(2012,01,04));
listofDates.add(new DateTime(2012,01,05));
listofDates.add(new DateTime(2012,01,06));
..
listofDates.add(new DateTime(2012,01,31));
listofDates.add(new DateTime(2012,02,01));
..
listofDates.add(new DateTime(2012,02,28));
listofDates.add(new DateTime(2012,03,01));
..
listofDates.add(new DateTime(2012,03,16));

我需要按月份范围过滤上述列表,如下所示:

var filterdListByMonth = listofDates.Where(x.DateTime.Month => 1 and x.DateTime.Month <=2).ToList();

但这似乎并没有返回正确的结果。有人可以帮忙吗?

4

1 回答 1

4

如果您修复编译器错误,它会按预期工作:

List<DateTime> listOfDates = new List<DateTime>();
listOfDates.Add(new DateTime(2012, 01, 04));
listOfDates.Add(new DateTime(2012, 01, 05));
listOfDates.Add(new DateTime(2012, 01, 06));
listOfDates.Add(new DateTime(2012, 01, 31));
listOfDates.Add(new DateTime(2012, 02, 01));
listOfDates.Add(new DateTime(2012, 02, 28));
listOfDates.Add(new DateTime(2012, 03, 01));
listOfDates.Add(new DateTime(2012, 03, 16));
var filterdListByMonth = listOfDates.Where(date => date.Month >= 1 && date.Month <= 2);

// Outputs each date except the two in March.
Console.WriteLine(String.Join(Environment.NewLine, filterdListByMonth));
于 2012-09-09T11:32:12.657 回答