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

Posted IT-HourseMan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
}

以上是关于linq中怎么实现多条件关联的左右连接的主要内容,如果未能解决你的问题,请参考以下文章

c# linq 动态多条件查询语句的写法

linq 多条件排序,该怎么处理

hibernate关联关系(多对多)

LINQ中连接操作符 --两个条件

spring Data jpa 一对多关联 动态查询怎么写

sql 各种连接的使用条件,