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

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. JavaScript 的HTML转义方法 html_encode 和 html_decode

    此方法用来将用户输入内容中的尖括号.引号等进行转义  

  2. Jmeter+ant+jenkins接口自动化测试 平台搭建(一)

    平台简介 一个完整的接口自动化测试平台需要支持接口的自动执行,自动生成测试报告,以及持续集成.Jmeter 支持接口的测试,Ant 支持自动构建,而 Jenkins 支持持续集成,所以三者组合在一起可 ...

  3. 如何配置pycaffe

    首先,使用cmake配置.生成caffe的vs2015工程时,设定生成python接口,即BUILD项->BUILD_python.BUILD_python_layer,注意使用CMake生成V ...

  4. InsertionSort 直接插入排序(java)

    排序思想: 相当于一堆数字,一开始先取出2个数排序,2个数排好序之后,再从一堆数字里面取一个数排序,直到结束伪代码: INSERTION_SORT(A) for j = 2 to A.length k ...

  5. windows 平台安装 ffmpeg

    一.从https://ffmpeg.zeranoe.com/builds/中下载ffmpeg的static版本: 二.将下载下来的“ffmpeg-4.0.2-win64-static.zip”解压到任 ...

  6. Windows ,获取硬盘物理序列号(VC++)

    #include <windows.h> BOOL GetHDID(PCHAR pIDBufer) {     HANDLE hDevice=NULL;    hDevice=::Crea ...

  7. 服务器与Linux操作系统基础原理

    1.服务器 2.Linux操作系统 1. 服务器 服务器定义与分类: 定义:一个管理资源并为用户提供服务的计算机软件. 按应用分类:通常分为文件服务器(能使用户在其它计算机访问文件),数据库服务器和应 ...

  8. Python20-Day05

    一.模块与包 1.模块 什么是模块? 在python中,模块可以分为四个通用类别: 1. 使用python编写的.py文件 2. 已经被编译为共享库或DLL的c或者c++扩展 3. 把一系列模块组织到 ...

  9. -lPods-NewsPushClientSDK is not an object file (not allowed in a library)

    今天在给客户定制SDK的过程中,出现了下面的一个问题,具有代表性,现在记录下来 问题: Showing All Errors Only : /Applications/Xcode.app/Conten ...

  10. Java每日编程day2

    Java每日编程day2 第一题 package com.pcx.day2; /* * 九九乘法表并逆序 */ public class JiuJiu { public static void mai ...