2016-09-22 47 views
1

我试过重复不同的事情来做到这一点。 我需要查询数据库并只返回列表中包含日期的记录 - 就像动态的“Where”语句一样。 我相信它会涉及=>但无法获得正确的语法。Linq以实体返回包含来自另一个列表中的元素的列表

我在下面建立一个短列表来测试,但它可以有任何数量的项目。

下面只需返回datesToShow中包含total.date_reference的记录。

  List<DateTime> datesToShow = new List<DateTime>(); 
     datesToShow.Add(new DateTime(2016, 9, 22)); 
     datesToShow.Add(new DateTime(2016, 9, 21)); 

     var todays_totals = (from total in dbContext.daily_totals 
           select new 
          { 
           total.customer.customer_name, 
           total.date_reference, 
           total.EDI_PODs_sent, 
           total.open_stops, 
           total.total_pieces, 
           total.new_stops, 
           total.total_weight, 
           total.Unsent_PODs_released, 
           total.Unsent_PODs_unreleased, 
           total.last_updated 
          }).ToArray(); 

如果我这样做:

var todays_totals = (from total in dbContext.daily_totals 
          select new 
          { 
           total.customer.customer_name, 
           total.date_reference, 
           total.EDI_PODs_sent, 
           total.open_stops, 
           total.total_pieces, 
           total.new_stops, 
           total.total_weight, 
           total.Unsent_PODs_released, 
           total.Unsent_PODs_unreleased, 
           total.last_updated 
          }).Where(el => datesToShow.Contains(el.date_reference)).ToArray(); 

我得到一个 “未知的方法在哪里?(?)” 我一直在使用这两个列表,并像一个数组的尝试:

DateTime[] datesToShow = new DateTime[] 
     { 
      new DateTime (2016,9,22), 
      new DateTime (2016,9,23) 
     }; 

我也会很满意一个新的结果集,这是todays_totals的一个子集。 类似下面的(这里我其实从开始)

var newList = (from t in todays_totals where (t=>datesToShow.Contains(t.date_reference))).ToArray(); 
+0

是你反对使用lambda表达式? –

+0

完全没有BviLLeKid –

回答

2

您可以尝试使用Contains扩展方法:

 var todays_totals = (from total in dbContext.daily_totals 
          where datesToShow.Contains(DbFunctions.TruncateTime(total.date_reference)) 
          select new 
         { 
          total.customer.customer_name, 
          total.date_reference, 
          total.EDI_PODs_sent, 
          total.open_stops, 
          total.total_pieces, 
          total.new_stops, 
          total.total_weight, 
          total.Unsent_PODs_released, 
          total.Unsent_PODs_unreleased, 
          total.last_updated 
         }).ToArray(); 

DbFunction.TruncateTime会帮助你清理你的日期在他们与时间的情况下。

+0

不管我如何尝试并输入这一个,我得到一个错误,如果我保留DbFunctions那么我得到一个未知的方法在哪里(?),即使我删除TruncateTime部分,我也会得到同样的结果。 –

+0

请发布错误以查看发生了什么 – octavioccl

+0

还有第二个错误:错误参数1:无法从'System.DateTime?'转换到'System.DateTime'\t - 这似乎是指向的方式,但...? –

0

使用Lambda表达式:

List<DateTime> datesToShow = new List<DateTime>(); 

datesToShow.Add(new DateTime(2016, 9, 22)); 
datesToShow.Add(new DateTime(2016, 9, 21)); 

var todays_totals = dbContext.daily_totals.Where(o=> datesToShows.Contains(o.date_reference)).ToList(); 

//Result should be a list that contains records that match those 2 dates. 

希望这有助于!

0

这里有两种方法可以做到你想要什么,是basicly相同:

  1. 使用LINQ where声明。他们可以返回任何有效的C#表达bool

    (from total in dbContext.daily_totals 
    where datesToShow.Contains(total.date_reference) 
    select new 
    { 
        // your select items... 
    }).ToArray(); 
    
  2. WHERE条款,即意志,正如你所指出,包含lambda表达式使用LINQ扩展方法:

    (from total in dbContext.daily_totals 
    select new 
    { 
        // your select items... 
    }) 
    .Where(el => datesToShow.Contains(el.date_reference)) 
    .ToArray(); 
    
+0

任何时候我用“where datesToShow.Contains(total.date_reference)”我得到一个未知的成员'date_reference'错误。 –

相关问题