2017-05-26 38 views
0

我得到重複,這裏飯菜LINQ留下過濾加盟加盟

IEnumerable<DTOHotMealsPrice> lst = (from m in this.dbEntity.HOT_MEALS 
    join ml in this.dbEntity.HOT_MEALS_PRICE on m.MEALSID equals ml.MEALSID into mls 
    from mls1 in mls.DefaultIfEmpty() 
    where mls1.HOTID==hotelId 
    select new DTOHotMealsPrice 
    { 
     MEALSID = m.MEALSID, 
     MEALSNAME = m.MEALSNAME, 
     CHPRICE = mls1.CHPRICE, 
     PRICE = mls1.PRICE, 
     HOTID = mls1.HOTID 
    }).Distinct().ToList(); 

我想列出所有HOT_MEALS並與 HOT_MEALS_PRICE加入時,就可以了mealsid參考

mls1.HOTID==hotelId,這將越來越innerjoin結果 怎麼可能將一個正確的結果

+0

請提供你的表結構。 –

+0

'public partial class HOT_MEALS { public short MEALSID {get;組; } public string MEALSNAME {get;組; } } public partial class HOT_MEALS_PRICE { public short MEALSID {get;組; } public int HOTID {get;組; } public Nullable PRICE {get;組; } public Nullable CHPRICE {get;組; ' }' –

回答

0

雙向解決方案

刪除哪裏案例

加入其中案例在加入表

顯示在哪裏案例吹LINQ查詢

第一查詢

IEnumerable<DTOHotMealsPrice> lst = (from m in this.dbEntity.HOT_MEALS 
join ml in this.dbEntity.HOT_MEALS_PRICE.where(c=>c.HOTID==hotelId) on m.MEALSID equals ml.MEALSID into mls 
from mls1 in mls.DefaultIfEmpty() 
select new DTOHotMealsPrice 
{ 
    MEALSID = m.MEALSID, 
    MEALSNAME = m.MEALSNAME, 
    CHPRICE = mls1.CHPRICE, 
    PRICE = mls1.PRICE, 
    HOTID = mls1.HOTID 
}).Distinct().ToList(); 

二是:

IEnumerable<DTOHotMealsPrice> lst = (from m in this.dbEntity.HOT_MEALS 
join ml in this.dbEntity.HOT_MEALS_PRICE on m.MEALSID equals ml.MEALSID into mls 
from mls1 in mls.where(c=>c.HOTID==hotelId).DefaultIfEmpty() 
select new DTOHotMealsPrice 
{ 
    MEALSID = m.MEALSID, 
    MEALSNAME = m.MEALSNAME, 
    CHPRICE = mls1.CHPRICE, 
    PRICE = mls1.PRICE, 
    HOTID = mls1.HOTID 
}).Distinct().ToList();