C# Linq to entities返回包含来自另一个列表的元素的列表

C# Linq to entities返回包含来自另一个列表的元素的列表,c#,entity-framework,linq,C#,Entity Framework,Linq,我尝试过重复不同的事情来做到这一点。 我需要查询数据库,只返回列表中包含日期的记录,就像动态“Where”语句一样。 我确信它将涉及=>但无法获得正确的语法 我在下面建立一个简短的列表进行测试,但它可以有任何数量的项目 下面只需返回datesToShow中包含total.date_引用的记录 List<DateTime> datesToShow = new List<DateTime>(); datesToShow.Add(new Da

我尝试过重复不同的事情来做到这一点。 我需要查询数据库,只返回列表中包含日期的记录,就像动态“Where”语句一样。 我确信它将涉及=>但无法获得正确的语法

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

下面只需返回datesToShow中包含total.date_引用的记录

          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();
我得到一个“未知方法,其中(?)” 我尝试使用列表和数组,如:

   DateTime[] datesToShow = new DateTime[] 
        {
            new DateTime (2016,9,22),
            new DateTime (2016,9,23)
        };
我也可以使用一个新的结果集,该结果集是今天的总和的子集。 类似下面的内容(我实际上是从这里开始的)


您可以尝试使用
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();

将帮助您清理日期,以防日期随时间而来。

使用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.
(from total in dbContext.daily_totals
select new
{
    // your select items...
})
.Where(el => datesToShow.Contains(el.date_reference))
.ToArray();
List datesToShow=new List();
datesToShow.Add(新日期时间(2016年9月22日));
datesToShow.Add(新日期时间(2016年9月21日));
var todays_totals=dbContext.daily_totals.Where(o=>datesToShows.Contains(o.date_reference)).ToList();
//结果应该是一个列表,其中包含与这两个日期匹配的记录。

希望这有帮助

有两种方法可以做你想做的事,基本上是一样的:

  • 使用LINQ
    where
    语句。它们可以接受任何返回
    bool
    的有效C#表达式:

    (from total in dbContext.daily_totals
    where datesToShow.Contains(total.date_reference)
    select new
    {
        // your select items...
    }).ToArray();
    
  • WHERE
    子句使用LINQ扩展方法,正如您所指出的,该子句将包含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.
    
    (from total in dbContext.daily_totals
    select new
    {
        // your select items...
    })
    .Where(el => datesToShow.Contains(el.date_reference))
    .ToArray();
    

  • 你反对使用lambda表达式吗?一点也不反对。每当我使用“where datesToShow.Contains(total.date\u reference)”时,我会得到一个未知的成员“date\u reference”错误。无论我如何尝试输入这个,我都会得到一个错误,如果我将DbFunctions保留在那里,那么我会得到一个未知的方法,其中(?)即使我删除了TruncateTime部分,我也会得到同样的结果。还有第二个错误:错误2参数1:无法从'System.DateTime'转换为'System.DateTime',这似乎指明了方向,但是…?哦,这是一个编译错误,但是
    total.date\u reference
    不是
    DateTime
    属性?这就是交易,是可为空的类型,请尝试将列表转换为
    list datesToShow=new list()或者您可以将where条件更改为:
    total.date\u reference=null&&datesToShow.Contains(total.date\u reference.Value)
    执行此操作,将列表更改为null可修复此错误。非常感谢。