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

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. AJAX其实就是一个异步网络请求

    AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML).其实就是一个异步网络请求. 一.创建对象 var xmlhttp; if (w ...

  2. Hyperledger Fabric 1.0 从零开始(一)

    在HyperLedger/Fabric发布0.6的时候,公司就已经安排了一个团队研究这一块,后来也请IBM的专家组过来培训了一批人,不幸的是,这批人后来全走了,然后1.0就发布了.自从2017年7月H ...

  3. MATLAB2018a与2016b分类学习模型Java库向上兼容操作

    matlab2016版本无法兼容matlab2018版本Classification Learner创建的分类器 在java环境下,使用matlab2016版本调用matlab2018版本Classi ...

  4. CS224n-作业1

    0 前言 作业1对应的试题 作业1对应的启动代码 作业1主页 1 Softmax(10分) (a)(5分) 对于向量$x+c$的任一维度$i$,有: \begin{align*}\mbox{softm ...

  5. FileZilla-FTP连接失败

    状态: 已登录状态: 读取“/”的目录列表...命令: CWD /响应: 250 CWD successful. "/" is current directory.命令: TYPE ...

  6. uniq命令详解

    基础命令学习目录首页 原文链接:http://man.linuxde.net/uniq 删除重复行: uniq file.txt sort file.txt | uniq sort -u file.t ...

  7. 开发简单的IO多路复用web框架

    自制web框架 1.核心IO多路复用部分 # -*- coding:utf-8 -*- import socket import select class Snow(): def __init__(s ...

  8. 王者荣耀交流协会PSP Daily项目Postmortem结果

    王者荣耀交流协会PSP Daily项目Postmortem结果 整理:王超 设想和目标 1.       我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? PSP D ...

  9. “Hello World!”团队第七周召开的第五次会议

    今天是我们团队“Hello World!”团队第七周召开的第五次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八.代码 一 ...

  10. 获取session

    HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()) ...