簡體   English   中英

Linq 3表組聯接DefaultifEmpty查詢

[英]Linq 3 Table GroupJoin DefaultifEmpty Query

我的桌子

旅館

ID
名稱

價錢

PriceID酒店ID
價格(小數)開始日期

折扣

HotelID DiscountID DiscountType DiscountRate

關系酒店和價格1 xn關系酒店和價格1 xn

但是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 = ??
                    )});

怎么寫查詢? 單一酒店,單一價格或NULL,單一折扣或空

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM