使用单个参数 Id 搜索记录列表并与多列 EF 进行比较
Searching Record List using single parameter Id and compare with multiple columns EF
我想从 table 获取记录,但我只有一个参数 ID。我想将单个参数与多列进行比较。这是我的代码。
var list = _ctx.Scheduler.Where(x => x.SaloonId == Id).ToList().Select(x => new AppointmentListModel
{
Id = x.ID,
StatusName = x.AppointmentStatus.StatusName,
AppointmentDate = x.AppointmentDate.ToShortDateString(),
AppointmentDay = x.AppointmentDay,
CustomerName = x.CustomerDetail.UserMaster.FirstName + " " + x.CustomerDetail.UserMaster.LastName,
FromTime = x.AppointmentTimeFrom,
ToTime = x.AppointmentTimeTo
}).ToList();
上面的查询是从调度程序 table 获取有关沙龙 ID 的约会。这是下一个查询,用于从相同的 table 获取记录,但与 customerId.
进行比较
var list = _ctx.Scheduler.Where(x => x.CustomerId == Id).ToList().Select(x => new AppointmentListModel
{
Id = x.ID,
StatusName = x.AppointmentStatus.StatusName,
AppointmentDate = x.AppointmentDate.ToShortDateString(),
AppointmentDay = x.AppointmentDay,
CustomerName = x.CustomerDetail.UserMaster.FirstName + " " + x.CustomerDetail.UserMaster.LastName,
FromTime = x.AppointmentTimeFrom,
ToTime = x.AppointmentTimeTo
}).ToList();
如何使用单个查询来实现。
尝试使用 conditional logical OR (||
) operator:
var list = _ctx.Scheduler
.Where(x => x.SaloonId == Id || x.CustomerId == Id)
.ToList()
.Select(x => new AppointmentListModel
{
Id = x.ID,
StatusName = x.AppointmentStatus.StatusName,
AppointmentDate = x.AppointmentDate.ToShortDateString(),
AppointmentDay = x.AppointmentDay,
CustomerName = x.CustomerDetail.UserMaster.FirstName + " " + x.CustomerDetail.UserMaster.LastName,
FromTime = x.AppointmentTimeFrom,
ToTime = x.AppointmentTimeTo
})
.ToList();
您只需将这两个条件都添加到 Where 子句中即可。
var list = _ctx.Scheduler.Where(x => x.SaloonId == Id || x.CustomerId == Id).ToList().Select(x => new AppointmentListModel
{
Id = x.ID,
StatusName = x.AppointmentStatus.StatusName,
AppointmentDate = x.AppointmentDate.ToShortDateString(),
AppointmentDay = x.AppointmentDay,
CustomerName = x.CustomerDetail.UserMaster.FirstName + " " + x.CustomerDetail.UserMaster.LastName,
FromTime = x.AppointmentTimeFrom,
ToTime = x.AppointmentTimeTo
}).ToList();
我想从 table 获取记录,但我只有一个参数 ID。我想将单个参数与多列进行比较。这是我的代码。
var list = _ctx.Scheduler.Where(x => x.SaloonId == Id).ToList().Select(x => new AppointmentListModel
{
Id = x.ID,
StatusName = x.AppointmentStatus.StatusName,
AppointmentDate = x.AppointmentDate.ToShortDateString(),
AppointmentDay = x.AppointmentDay,
CustomerName = x.CustomerDetail.UserMaster.FirstName + " " + x.CustomerDetail.UserMaster.LastName,
FromTime = x.AppointmentTimeFrom,
ToTime = x.AppointmentTimeTo
}).ToList();
上面的查询是从调度程序 table 获取有关沙龙 ID 的约会。这是下一个查询,用于从相同的 table 获取记录,但与 customerId.
进行比较var list = _ctx.Scheduler.Where(x => x.CustomerId == Id).ToList().Select(x => new AppointmentListModel
{
Id = x.ID,
StatusName = x.AppointmentStatus.StatusName,
AppointmentDate = x.AppointmentDate.ToShortDateString(),
AppointmentDay = x.AppointmentDay,
CustomerName = x.CustomerDetail.UserMaster.FirstName + " " + x.CustomerDetail.UserMaster.LastName,
FromTime = x.AppointmentTimeFrom,
ToTime = x.AppointmentTimeTo
}).ToList();
如何使用单个查询来实现。
尝试使用 conditional logical OR (||
) operator:
var list = _ctx.Scheduler
.Where(x => x.SaloonId == Id || x.CustomerId == Id)
.ToList()
.Select(x => new AppointmentListModel
{
Id = x.ID,
StatusName = x.AppointmentStatus.StatusName,
AppointmentDate = x.AppointmentDate.ToShortDateString(),
AppointmentDay = x.AppointmentDay,
CustomerName = x.CustomerDetail.UserMaster.FirstName + " " + x.CustomerDetail.UserMaster.LastName,
FromTime = x.AppointmentTimeFrom,
ToTime = x.AppointmentTimeTo
})
.ToList();
您只需将这两个条件都添加到 Where 子句中即可。
var list = _ctx.Scheduler.Where(x => x.SaloonId == Id || x.CustomerId == Id).ToList().Select(x => new AppointmentListModel
{
Id = x.ID,
StatusName = x.AppointmentStatus.StatusName,
AppointmentDate = x.AppointmentDate.ToShortDateString(),
AppointmentDay = x.AppointmentDay,
CustomerName = x.CustomerDetail.UserMaster.FirstName + " " + x.CustomerDetail.UserMaster.LastName,
FromTime = x.AppointmentTimeFrom,
ToTime = x.AppointmentTimeTo
}).ToList();