Linq 3 Table GroupJoin DefaultifEmpty Query

时间:2012-01-04 17:01:54

标签: c# linq defaultifempty

我的桌子

饭店

ID
姓名

价格

PriceID HotelID
价格(十进制) 起始日期

折扣

HotelID DiscountID DiscountType DiscountRate

关系酒店和价格1 x n 关系酒店和折扣1 x n

但价格可以为空,折扣可以为空

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

如何编写查询?单人酒店,单人价格或零,单一折扣或零

1 个答案:

答案 0 :(得分:0)

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
}