C# 如何将linq与两个相关实体一起使用?
您好,我在dotnet项目中使用两个相关实体时遇到问题C# 如何将linq与两个相关实体一起使用?,c#,entity-framework,linq,C#,Entity Framework,Linq,您好,我在dotnet项目中使用两个相关实体时遇到问题 我有两个实体表和预订,我需要获得为明天预订的表,但日期在预订表中设置 这是密码 public class Table { public int Id { get; set; } public bool isAvailable { get; set; } public int Numero { get; set; } public virtual ICollection<Reservation> I
我有两个实体表和预订,我需要获得为明天预订的表,但日期在预订表中设置 这是密码
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() {
....
}
但似乎有一个错误
Argument1:无法从int转换为Reservation
您可以尝试在查询中使用导航,如:
return rc.tReservation
.Include(reservation => reservation.Table)
.Where(r => (r.DateReservation == DateTime.Today.AddDays(1)))
.Select(reservation => reservation.table).ToList();
您可以尝试在查询中使用导航,如:
return rc.tReservation
.Include(reservation => reservation.Table)
.Where(r => (r.DateReservation == DateTime.Today.AddDays(1)))
.Select(reservation => reservation.table).ToList();
我假设您收到一个LINQtoEntities 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);
}
请参阅以下完整的解决方案:
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);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Data.Entity;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间ReservationTableIssue
{
班级计划
{
静态void Main(字符串[]参数)
{
TableRepository repo=新建TableRepository();
//步骤#1-计算出您需要在24小时内找到为其保留的表
//按日期搜索时,建议使用日期范围而不是一次特定日期
//开始日期和结束日期将满足明天24小时的要求。
//明天开始
DateTime startDate=新的日期时间(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,0,0).AddDays(1);
//明天结束
DateTime endDate=新的日期时间(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,0,0).AddDays(2);
//使用日期范围(明天的24小时期限)调用存储库方法
var tablesForTomorrow=repo.GetAllReservedTables(开始日期、结束日期);
//在控制台中显示数据
foreach(表fortomorrow中的var表)
{
Console.WriteLine(“表号:{0}”,Table.Numero);
}
Console.WriteLine(“按任意键退出”);
Console.ReadKey();
}
}
公共类表
{
公共int Id{get;set;}
公共布尔值可用{get;set;}
公共整数{get;set;}
公共虚拟ICollection定向观测{get;set;}
}
公务舱预订
{
公共日期时间日期保留{get;set;}
公共int Id{get;set;}
公共字符串Nom{get;set;}
公共虚拟表{get;set;}
}
公共类RestaurantContext:DbContext
{
公共数据库集tTable{set;get;}
公共数据库集tReservation{set;get;}
public RestaurantContext():base(“restaurantdb”){
}
}
公共类表存储库
{
RestaurantContext rc=null;
公共表存储库()
{
rc=新餐厅上下文();
}
公共无效提交()
{
rc.SaveChanges();
}
公共无效添加表(表m)
{
rc.tTable.Add(m);
}
公共IEnumerable GetAllTables()
{
返回rc.tTable.ToList();
}
///
///一种方法,允许您获取在特定时间段内保留的所有表。仅当保留了表时,才会返回这些表。
///
///
///
///
public IEnumerable GetAllReservedTables(日期时间开始,日期时间结束)
{
返回此.rc.tReservation
//按日期范围筛选
其中(x=>x.DateReservation>=start&&x.DateReservation x.table);
}
}
}
我假设您接收到linq to entities 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);
}
请参阅以下完整的解决方案:
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);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Data.Entity;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间ReservationTableIssue
{
班级计划
{
静态void Main(字符串[]参数)
{
TableRepository repo=新建TableRepository();
//步骤#1-计算出您需要在24小时内找到为其保留的表
//按日期搜索时,建议使用日期范围而不是一次特定日期
//开始日期和结束日期将满足明天24小时的要求。
//明天开始
DateTime startDate=新的日期时间(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,0,0).AddDays(1);
//明天结束
DateTime endDate=新的日期时间(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,0,0).AddDays(2);
//使用日期范围(明天的24小时期限)调用存储库方法
var tablesForTomorrow=repo.GetAllReservedTables(开始日期、结束日期);
//在控制台中显示数据
foreach(表fortomorrow中的var表)
{
Console.WriteLine(“表号:{0}”,Table.Numero);
}
Console.WriteLine(“按任意键退出”);
Console.ReadKey();
}
}
公共类表
{
公共int Id{get;set;}
公共布尔值可用{get;set;}
公共整数{get;set;}
公共虚拟ICollection定向观测{get;set;}
}
公务舱预订
{
公共日期时间日期保留{get;set;}
公共int Id{get;set;}
公共字符串Nom{get;set;}
公共虚拟表{get;set;}
}
公共类RestaurantContext:DbContext
{
公共数据库集t