//第一个集合为所有的数据

var specilist = new List<Me.SpecificationsInfo>();

var resultall = (from a in dbContext.by_sku_items
                                        join b in dbContext.by_attributes on a.by_attributes_id equals b.by_attributes_id
                                        join c in dbContext.by_attribute_values on a.by_attribute_values_id equals c.by_attribute_values_id
                                        where a.by_product_id == proid
                                        select new Me.OrderEditAll
                                        {
                                            skuid = a.by_sku_items_id,
                                            colorid = b.by_attributes_id,
                                            color = b.attributes_name,
                                            sizeid = c.by_attribute_values_id,
                                            size = c.value_str
                                        }).ToList();

//第二个集合为需要合并的集合
                        var orderitemlist = dbContext.by_order_item.Where(t => t.order_id == _orderid && t.by_product_id == proid).ToList();
                        var resultorder = (from a in orderitemlist
                                     join b in dbContext.by_sku_items on a.by_sku_items_id equals b.by_sku_items_id
                                     join c in dbContext.by_attributes on b.by_attributes_id equals c.by_attributes_id
                                     join d in dbContext.by_attribute_values on b.by_attribute_values_id equals d.by_attribute_values_id
                                     //where b.status == statusWorking && c.status == statusWorking && d.status == statusWorking
                                     orderby c.index, d.index
                                     select new Me.OrderEdit
                                     {
                                         skuid = a.by_sku_items_id,
                                         id = a.by_order_item_id,
                                         nums = a.nums,
                                         colorid = c.by_attributes_id,
                                         color = c.attributes_name,
                                         sizeid = d.by_attribute_values_id,
                                         size = d.value_str
                                     }).ToList();

//第三个为最终结果,将不为空的数据合并到所有集合中
                        var result = (from all in resultall
                                     join order in resultorder on all.skuid equals order.skuid into temp
                                     from tt in temp.DefaultIfEmpty()
                                     select new Me.OrderEdit
                                     {
                                         skuid = all.skuid,
                                         id = tt != null ? tt.id : 0,
                                         nums = tt != null ? tt.nums : 0,
                                         colorid = all.colorid,
                                         color = all.color,
                                         sizeid = all.sizeid,
                                         size = all.size

}).ToList();

//第二种为sql实现方式

select by_sku_items_id,sum(t1.by_order_items_id) as by_order_items_id,sum(t1.nums) as nums,t1.by_attributes_id,t1.attributes_name,t1.by_attribute_values_id,t1.value_str from 
(
select  * from (
select * from 
(select a.by_sku_items_id,0 as by_order_items_id,0 as nums,b.by_attributes_id,b.attributes_name,c.by_attribute_values_id,c.value_str from by_sku_items a
join by_attributes b on a.by_attributes_id=b.by_attributes_id
join by_attribute_values c on a.by_attribute_values_id=c.by_attribute_values_id
where by_product_id=9 ) aa
UNION 
select * from (
select a.by_sku_items_id,a.by_order_item_id,a.nums,c.by_attributes_id,c.attributes_name,d.by_attribute_values_id,d.value_str
from by_order_item a
join by_sku_items b on a.by_sku_items_id=b.by_sku_items_id
join by_attributes c on b.by_attributes_id=c.by_attributes_id
join by_attribute_values d on b.by_attribute_values_id=d.by_attribute_values_id
where b.status=2 and c.status=2 and d.status=2 
and a.order_id=1459428164000004 and a.by_product_id=9
) bb
) t 
) t1 group by by_sku_items_id,by_attributes_id,by_attribute_values_id,attributes_name,value_str

linq 左连接实现两个集合的合并的更多相关文章

  1. GroupBy分组的运用和linq左连接

    最简单的分组 var conHistoryList = conHistoryData.GroupBy(g => g.personId); 就是conHistoryData是一个IQueryabl ...

  2. Linq Left Join;linq左连接 (转载)

    来源 https://www.cnblogs.com/xinjian/archive/2010/11/17/1879959.html 准备一些测试数据,如下: use Test Create tabl ...

  3. mysql---union和左连接的两倒面试题

    第一道: 思路:无非是将hid与gid与t表中的tname关联起来.实质上是三表关联(m,t,t) 先将hid与tname关联起来,运用左连接 再将结果集与t表中的tname关联起来,使得gid与tn ...

  4. C# linq左连接与分组

    1.左连接使用DefaultIfEmpty(): 2.分组时候判断newper.FirstOrDefault() == null ? null: newper.ToList()这个经常出错误,如果不判 ...

  5. linq 左连接后实现与主表一对一关系数据

    var query1 = from r in _residentRepository.GetAll() join i in _inLogRepository.GetAll() on r.Id equa ...

  6. EF to linq 左连接

    如果连接的数据不存在用 null 表示,则可以左连接查询,但是如果数据类型为 int 则会出错. var ng = (from g in _db.NET_NEWS_GROUP join z in _d ...

  7. Linq 左连接 left join

    Suppose you have a tblRoom and tblUserInfo. Now, you need to select all the rooms regardless of whet ...

  8. linq左连接

    Table1和Table2连接,把Table1的全列出来 var tempData = from a in table1 join b in table2 on a.Id equals b.aId i ...

  9. linq 左连接

    var list = (from item in vall join item3 in v1 on new { item.FItemID, item.FAuxPropID } equals new { ...

随机推荐

  1. js问题 window.location.hash和window.location.href有什么不同

    hash:设置或获取 href 属性中在井号“#”后面的分段. href:设置或获取整个 URL 为字符串. 通过下面的测试你会发现区别,将代码放到你的HTML中,然后用浏览器打开,测试步骤: 点击“ ...

  2. mysql 中sql语句关键字的书写顺序与执行顺序

    书写顺序: select -> from -> where -> group by -> having -> order by 执行顺序: from -> wher ...

  3. java随记

    jdk1.8中新增了 LocalDate 与 LocalDateTime等类来解决日期处理方法,同时引入了一个新的类DateTimeFormatter来解决日期格式化问题. LocalDateTime ...

  4. Python-RabbitMQ(持久化)

    生产者: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import pika   connection = pika.BlockingConnection(pi ...

  5. 解读Python编程中的命名空间与作用域

    变量是拥有匹配对象的名字(标识符).命名空间是一个包含了变量名称们(键)和它们各自相应的对象们(值)的字典.一个Python表达式可以访问局部命名空间和全局命名空间里的变量.如果一个局部变量和一个全局 ...

  6. jenkins展示report测试报告的配置

    HTML报告展示 1. 需要HTML Publisher plugin插件 2. 在workspace下的工程(构建)中的目录中存储测试报告 在Jenkins中新建一个job,进入配置项. 首先通过p ...

  7. maven实战读书笔记(三)

    maven将一系列的步骤都封装为一系列的插件,运行命令后一系列的插件运行

  8. 王者荣耀交流协会final发布文案美工展示博客

    logo: 我们的logo是蓝底白字,非常简洁大气的设计感,上面印有我们的软件名称,更好的直观的彰显了我们的主题.我们的软件就是要迎合使用者,给使用者更加方便快捷的工作体验,更好的衡量自己的时间分配. ...

  9. bootstrap table的展开行问题

    照着网上与api里说的添加detailView属性设置为true,detailFormatter属性为展开后的内容,但是设置之后发现,在表格每一行最前面是多出一列正常该显示"+"的 ...

  10. c#窗体移动与窗体阴影效果

    //步骤//1.导入代码//2.事件中添加form_1mousedown函数//3.在load函数中定义AnimateWindow语句,注意有两个引用.//4.DllImport若有错误,按shift ...