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 }