Linq to实体返回包含来自另一个列表的元素的列表

时间:2016-09-22 19:45:41

标签: c# entity-framework linq

我尝试过多次不同的事情。 我需要查询数据库并仅返回包含在List中的日期的记录 - 如动态“Where”语句。 我相信它会涉及=>但无法使语法正确。

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

下面只需返回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();

3 个答案:

答案 0 :(得分: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会帮助您清理日期,以防它们及时出现。

答案 1 :(得分:0)

使用Lambda表达式:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:textAlignment="center"
        android:id="@+id/textView"
        android:text="@string/appIntro"/>

</LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="16sp"
        android:layout_marginEnd="1dp"
        android:layout_marginStart="2dp"
        android:text="@string/noOfUnitsMsg"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginEnd="2dp"
        android:layout_marginStart="1dp"
        android:digits="0123456789"
        android:inputType="number"
        android:id="@+id/input"
        android:textSize="16sp"
        android:maxLength="3"
        android:hint="@string/input"/>

    </LinearLayout>


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/calculate"
        android:id="@+id/calculate"
         />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="20dp"
        >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/resultMsg"
        android:textAlignment="center"
        android:layout_weight="1"
        android:textSize="20sp"
        android:layout_marginBottom="20dp"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/result"
        android:textAlignment="center"
        android:textSize="24sp"
        android:layout_weight="1"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="100dp"
        android:id="@+id/result"
        />
    </LinearLayout>
    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/clear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginEnd="1dp"
            android:layout_marginStart="2dp"
            android:textSize="16sp"
            android:text="@string/clear"
            tools:ignore="ButtonStyle" />

        <Button
            android:id="@+id/credits"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginEnd="1dp"
            android:layout_marginStart="1dp"
            android:textSize="16sp"
            android:text="@string/credits" />

        <Button
            android:id="@+id/exit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="2dp"
            android:layout_marginStart="1dp"
            android:layout_weight="1"
            android:textSize="16sp"
            android:text="@string/exit" />

    </LinearLayout>

</LinearLayout>

希望这有帮助!

答案 2 :(得分:0)

有两种方法可以做你想要的,基本相同:

  1. 使用LINQ where语句。他们可以使用任何返回bool的有效C#表达式:

    (from total in dbContext.daily_totals
    where datesToShow.Contains(total.date_reference)
    select new
    {
        // your select items...
    }).ToArray();
    
  2. WHERE子句使用LINQ扩展方法,正如您所指出的那样,它将包含一个lambda表达式:

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