提问者:小点点

排除带有LINQ的日期


我有以下LINQ查询,这一个对我很有用

List<Vehiculos> ob = JsonConvert.DeserializeObject<List<Vehiculos>>(response.Content);
          var m = ob.Max(x => x.number);
                Num = m;
                dataGridView5.DataSource = (from o in ob
                                            where o.number == m  
                                            orderby o.number descending
                                            select o).ToList();

现在我需要的是排除一个月,即告诉查询不要向我显示1月和2月的月份。TallerDate包含一个列表的日期,即,在那个TallerDate中有几个日期为一月、二月和三月的记录。我在这个查询中所做的是将最低的月份(即一月和三月)带入f中,但这并不是必须的,正如我之前所说的,我需要的是TallerDate不显示一月和二月的月份,而是显示三月以后的月份

List<Vehiculos> ob = JsonConvert.DeserializeObject<List<Vehiculos>>(response.Content);
  var f = ob.Min(x => x.workshopDate.Month);
  nvar m = ob.Max(x => x.number);
  Num = m;
  dataGridView5.DataSource = (from o in ob
                             where o.number == m && o.workshopDate.Month == f
                             orderby o.number descending
                             select o).ToList();

共1个答案

匿名用户

看起来您需要显示当月的记录,因此可以使用以下操作:

// this picks up the current month number
var currentMonth = DateTime.Now.Month;

dataGridView5.DataSource = (from o in ob
                         where o.number == m && o.workshopDate.Month == currentMonth 
                         orderby o.number descending
                         select o).ToList();

更准确地说,过滤记录越好:

var currentMonthFirstDate = new DateTime(DateTime.Now.Year,DateTime.Now.Month, 1);

dataGridView5.DataSource = (from o in ob
                         where o.number == m && o.workshopDate >= currentMonthFirstDate 
                         orderby o.number descending
                         select o).ToList();