码迷,mamicode.com
首页 > 其他好文 > 详细

linq中怎么实现多条件关联的左右连接

时间:2017-10-25 13:18:12      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:inf   sele   bsp   strong   into   linq   rom   关联   ext   

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
}

linq中怎么实现多条件关联的左右连接

标签:inf   sele   bsp   strong   into   linq   rom   关联   ext   

原文地址:http://www.cnblogs.com/dehuachenyunfei/p/7728094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!