Linq 3 Table GroupJoin DefaultifEmpty Query

本文关键字:DefaultifEmpty Query GroupJoin Table Linq | 更新日期: 2023-09-27 18:19:22

my Tables

酒店

ID
名称

价格

PriceIDHotelID
价格(十进制)StartDate可以

折扣

HotelIDDiscountIDDiscountTypeDiscountRate

酒店与价格的关系1 × n酒店与折扣的关系1 × n

但是price可以为Null, Discount可以为Null

var result = data.Hotels.GroupJoin(data.Prices, h => h.OtelID, p => p.OtelID, (h, p) => new { hotel = o, f = f.Where(x => x.StartDate<= Datetime.Now.date ).OrderBy(x => x.Price) })
                    .SelectMany(temp0 => temp0.f.DefaultIfEmpty(), (temp0, x) => new 
                    {
                        _hotel = temp0.hotel,
                        _Price = x,
                        _discount = ??
                    )});

怎么写查询?

Linq 3 Table GroupJoin DefaultifEmpty Query

var result = from h in data.Hotels
where true //where condition if any
select new 
{
_hotel = h,
_price = h.Prices.Where(p=>p).FirstOrDefault()//condition
_discount = h.Discounts.Where(true).FirstOrDefault() //condition
}