linq中怎么實現多條件關聯的左右連接


linq左右連接最重要的是DefaultIfEmpty()這個方法和join之后的表中判斷是否( temp != null)null,左右連接其實就是表的位置互換。

1、左連接:

from order in context.vab_OrderGoods.Where(i => i.deletef == 0 && i.order_id == orderId && i.input_type == 2)
join goods in context.mst_Goods.Where(k => k.eigyousyo_id == _eigyousyoId)
on order.goods_id equals goods.goods_id into ogt
from og in ogt.DefaultIfEmpty()
join kind in context.mst_GoodsKind.Where(l => l.deletef == 0 && l.eigyousyo_id == _eigyousyoId)
on og.goods_kind_id equals kind.goods_kind_id into tempTable
from temp in tempTable.DefaultIfEmpty()

select new FA2_GoodsInfoModel
{
seq_id = order.seq_id,
order_id = order.order_id,
goods_id = order.goods_id,
deletef = order.deletef,
goods_count = order.goods_count,
goods_name = og.goods_name,
goods_name_kana = og.goods_name_kana,
goods_weight = og.goods_weight,
input_type = order.input_type,
sub_total_goods_weight = order.sub_total_goods_weight,
goods_kind_id = temp != null ? temp.goods_kind_id : 0,
goods_kind_name = temp != null ? temp.goods_kind_name : string.Empty
}

2、右連接:

 

from goods in context.mst_Goods.Where(k => k.eigyousyo_id == _eigyousyoId)

join order in context.vab_OrderGoods.Where(i => i.deletef == 0 && i.order_id == orderId && i.input_type == 2)

on goods.goods_id equals order.goods_id into ogt
from og in ogt.DefaultIfEmpty()
join kind in context.mst_GoodsKind.Where(l => l.deletef == 0 && l.eigyousyo_id == _eigyousyoId)
on og.goods_kind_id equals kind.goods_kind_id into tempTable
from temp in tempTable.DefaultIfEmpty()

 

select new FA2_GoodsInfoModel
{
seq_id = order.seq_id,
order_id = order.order_id,
goods_id = order.goods_id,
deletef = order.deletef,
goods_count = order.goods_count,
goods_name = og.goods_name,
goods_name_kana = og.goods_name_kana,
goods_weight = og.goods_weight,
input_type = order.input_type,
sub_total_goods_weight = order.sub_total_goods_weight,
goods_kind_id = temp != null ? temp.goods_kind_id : 0,
goods_kind_name = temp != null ? temp.goods_kind_name : string.Empty
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM