2016-09-18 30 views
0

你好,我在一個dotnet項目中使用兩個相關的實體時遇到了問題
我有兩個實體表和預訂,我需要獲取明天預留的表,但日期是預留表 這裏是代碼如何在兩個相關實體中使用linq?

public class Table 
{ 
    public int Id { get; set; } 
    public bool isAvailable { get; set; } 
    public int Numero { get; set; } 
    public virtual ICollection<Reservation> IReservation { get; set; } 
} 

public class Reservation 
{ 
    public DateTime DateReservation { get; set; } 
    public int Id { get; set; } 
    public string Nom { get; set; } 
    public virtual Table table { get; set; } 
} 
public class RestaurantContext :DbContext 
{ 
    public DbSet<Table> tTable { set; get; } 
    public DbSet<Reservation> tReservation { set; get; } 
    public RestaurantContext() : base("RestaurentDB") {  
    } 
} 
class TableRepository 
{ 
    RestaurantContext rc = null; 

    public TableRepository() 
    { 
     rc = new RestaurantContext(); 
    } 
    public void Commit() 
    { 
     rc.SaveChanges(); 
    } 
    public void AddTable(Table m) 
    { 
     rc.tTable.Add(m); 
    } 

    public IEnumerable<Table> GetAllTables() { 
     return rc.tTable.ToList(); 
    } 
    public IEnumerable<Table> GetTablesReserverdTomorrow() { 
     .... 
    } 

在這裏,我需要得到這些將被保留明天 我試圖

var res=rc.tReservation.Where(r => (r.DateReservation == DateTime.Today.AddDays(1))).ToList(); 
      var res1 = rc.tTable.Select(r => res.Contains(r.Id)); 
      return res1; 

表,但似乎那裏有一個錯誤

ARGUMENT1:不能從int轉換爲預定

+0

請註明你 –

+0

對不起:)檢查編輯 –

+0

要獲得所有明天表中的誤差:返回rc.tReservation.Where(R => r.DateReservation == DateTime.Today.AddDays(1))選擇(R => r.Table).ToList(); –

回答

1

,你可以嘗試在查詢中使用的導航,如:

return rc.tReservation 
    .Include(reservation => reservation.Table) 
    .Where(r => (r.DateReservation == DateTime.Today.AddDays(1))) 
    .Select(reservation => reservation.table).ToList(); 
+0

說不能將lambda表達式轉換爲類型字符串,因爲它不是委託類型 –

+0

'return rc.tReservation 。Where(r =>(r.DateReservation == DateTime.Today.AddDays(1))) 。Select (預約=> reservation.table).ToList();'工作 –

+2

如何.INCLUDE取決於使用的EF版本使用。在舊版本中甚至沒有必要。 –

0

我假設你正在接受一個LINQ到實體SQL異常。這意味着你正在嘗試使用sql server中不可用的方法。

我已經採取了不同的方法,您的問題:

步驟#1:介紹了庫方法,人

/// <summary> 
/// A method that allows you to get all tables reserved in a specific period. tables are only returned if they are reserverd. 
/// </summary> 
/// <param name="start"></param> 
/// <param name="end"></param> 
/// <returns></returns> 
public IEnumerable<Table> GetAllReservedTables(DateTime start, DateTime end) 
{ 
    return this.rc.tReservation 
     // filter by date range 
     .Where(x => x.DateReservation >= start && x.DateReservation <= end) 

     // ensure table is returned 
     .Select(x => x.table); 
} 

第2步:調用庫方法,並確保你有合適的日期範圍,也就是明天:

TableRepository repo = new TableRepository(); 

    // figure out the 24 hour period you need to find reserved tables for 
    // it is advisible when searching by date to use a date range instead of once specific date 
    // the start date and end date will satisfy the 24 hour period of tomorrow. 

    // get start tomorrow 
    DateTime startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0).AddDays(1); 

    // get end of tomorrow 
    DateTime endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0).AddDays(2); 

    // call the repository method with the date range (the 24 hour period for tomorrow) 
    var tablesForTomorrow = repo.GetAllReservedTables(startDate, endDate); 

    // dispaly data in console 
    foreach(var table in tablesForTomorrow) 
    { 
     Console.WriteLine("Table Number: #{0}", table.Numero); 
    } 

請參見下面的完整的解決方案:

using System; 
using System.Collections.Generic; 
using System.Data.Entity; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ReservationTableIssue 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      TableRepository repo = new TableRepository(); 

      // step #1 - figure out the 24 hour period you need to find reserved tables for 
      // it is advisible when searching by date to use a date range instead of once specific date 
      // the start date and end date will satisfy the 24 hour period of tomorrow. 

      // get start tomorrow 
      DateTime startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0).AddDays(1); 

      // get end of tomorrow 
      DateTime endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0).AddDays(2); 

      // call the repository method with the date range (the 24 hour period for tomorrow) 
      var tablesForTomorrow = repo.GetAllReservedTables(startDate, endDate); 

      // dispaly data in console 
      foreach(var table in tablesForTomorrow) 
      { 
       Console.WriteLine("Table Number: #{0}", table.Numero); 
      } 

      Console.WriteLine("press any key to exit"); 
      Console.ReadKey(); 

     } 
    } 

    public class Table 
    { 
     public int Id { get; set; } 
     public bool isAvailable { get; set; } 
     public int Numero { get; set; } 
     public virtual ICollection<Reservation> IReservation { get; set; } 
    } 

    public class Reservation 
    { 
     public DateTime DateReservation { get; set; } 
     public int Id { get; set; } 
     public string Nom { get; set; } 
     public virtual Table table { get; set; } 
    } 
    public class RestaurantContext :DbContext 
    { 
     public DbSet<Table> tTable { set; get; } 
     public DbSet<Reservation> tReservation { set; get; } 
     public RestaurantContext() : base("RestaurentDB") {  
     } 
    } 

    public class TableRepository 
    { 
     RestaurantContext rc = null; 

     public TableRepository() 
     { 
      rc = new RestaurantContext(); 
     } 
     public void Commit() 
     { 
      rc.SaveChanges(); 
     } 
     public void AddTable(Table m) 
     { 
      rc.tTable.Add(m); 
     } 

     public IEnumerable<Table> GetAllTables() 
     { 
      return rc.tTable.ToList(); 
     } 

     /// <summary> 
     /// A method that allows you to get all tables reserved in a specific period. tables are only returned if they are reserverd. 
     /// </summary> 
     /// <param name="start"></param> 
     /// <param name="end"></param> 
     /// <returns></returns> 
     public IEnumerable<Table> GetAllReservedTables(DateTime start, DateTime end) 
     { 
      return this.rc.tReservation 
       // filter by date range 
       .Where(x => x.DateReservation >= start && x.DateReservation <= end) 

       // ensure table is returned 
       .Select(x => x.table); 
     } 
    } 

} 
1

在你的情況解析度是IEnumerable的,它包含了預定的情況下,不是int值。根據你的代碼的邏輯,似乎table和resrvation應該有相同的id來得到結果。 我想你應該改變你的代碼:

var res=rc.tReservation.Where(r => (r.DateReservation == DateTime.Today.AddDays(1))).ToList(); 
     var res1 = rc.tTable.Where(r => res.Any(resItem=>resItem.Id == r.Id)); 
     return res1;