首页 > 编程语言 >C#两个表多条件关联写法

C#两个表多条件关联写法

时间:2023-10-30 18:11:24浏览次数:36  
标签:表多 C# MaterialId Qty AfterOrderNo new 写法 public AfterQty

 

 一、两个表实体类准备
 public class OtherIn
        {
            public string AfterOrderNo { get; set; }
            public long MaterialId { get; set; }
            public string MaterNumber { get; set; }
            public long Qty { get; set; }
            public string Note { get; set; }
        }

        public class AfterInfo
        {
            public string AfterOrderNo { get; set; }
            public long MaterialId { get; set; }
            public string MaterNumber { get; set; }
            public long AfterQty { get; set; }
            public string Note { get; set; }
        }
View Code

 

二、实体类数据初始化

List<OtherIn> getOtherInGroup = new List<OtherIn>();
            getOtherInGroup.Add(new OtherIn 
            { 
                AfterOrderNo= "SH231025001",
                MaterialId=100001,
                MaterNumber="CTG001",
                Qty=2
            });
            getOtherInGroup.Add(new OtherIn
            {
                AfterOrderNo = "SH231025001",
                MaterialId = 100002,
                MaterNumber = "CTG002",
                Qty = 3
            });
            getOtherInGroup.Add(new OtherIn
            {
                AfterOrderNo = "SH231025002",
                MaterialId = 100002,
                MaterNumber = "CTG002",
                Qty = 4
            });
            List<AfterInfo> getAfterGroup = new List<AfterInfo>();
            getAfterGroup.Add(new AfterInfo 
            {
                AfterOrderNo = "SH231025002",
                MaterialId = 100002,
                MaterNumber = "CTG002",
                AfterQty = 5
            });
            getAfterGroup.Add(new AfterInfo
            {
                AfterOrderNo = "SH231025001",
                MaterialId = 100001,
                MaterNumber = "CTG001",
                AfterQty = 1
            });
View Code

 

三、第一种:sql的左关联 

FROM a JOIN b on 组合键关联 into 临时表  from 临时表(左关联不上右边默认为空)

  var getExcessGroup = from a in getOtherInGroup
                                 join b in getAfterGroup on new { a.AfterOrderNo, a.MaterialId } equals new { b.AfterOrderNo,b.MaterialId }
                                 into result
                                 from c in result.DefaultIfEmpty()
                                 select new
                                 {
                                     AfterOrderNo = a.AfterOrderNo,
                                     MaterialId=a.MaterialId,
                                     Qty = a.Qty,
                                     AfterQty = c == null ? 0 : c.AfterQty
                                 };

 结果

 

四、第二种相当于sql的 INNER JOIN

剔除了关联不上的

FROM a FROM b where 多条件关联

 var getExcessGroup2 = from a in getOtherInGroup
                                  from b in getAfterGroup
                                  where a.AfterOrderNo == b.AfterOrderNo && a.MaterialId == b.MaterialId
                                  select new
                                  {
                                      AfterOrderNo = a.AfterOrderNo,
                                      MaterialId = a.MaterialId,
                                      Qty = a.Qty,
                                      AfterQty = b == null ? 0 : b.AfterQty
                                  };

 结果

 FROM JOIN INTO

var getExcessGroup1 = from a in getOtherInGroup
                                 join b in getAfterGroup on new { a.AfterOrderNo, a.MaterialId } equals new { b.AfterOrderNo, b.MaterialId }
                                 into result
                                 from c in result
                                 select new
                                 {
                                     AfterOrderNo = a.AfterOrderNo,
                                     MaterialId = a.MaterialId,
                                     Qty = a.Qty,
                                     AfterQty = c == null ? 0 : c.AfterQty
                                 };

 

  FROM JOIN 省略into

var getExcessGroup11 = from a in getOtherInGroup
                                  join b in getAfterGroup on new { a.AfterOrderNo, a.MaterialId } equals new { b.AfterOrderNo, b.MaterialId }
                                  select new
                                  {
                                      AfterOrderNo = a.AfterOrderNo,
                                      MaterialId = a.MaterialId,
                                      Qty = a.Qty,
                                      AfterQty = b.AfterQty
                                  };

 

结果

 

标签:表多,C#,MaterialId,Qty,AfterOrderNo,new,写法,public,AfterQty
From: https://www.cnblogs.com/lanrenka/p/17791112.html

相关文章

  • QECon大会亮相产品,支持UI自动化测试?RunnerGo
    最近在gitee上看见一款获得GVP(最有价值开源项目)的测试平台RunnerGo,看他们官网介绍包含了接口测试、性能测试、自动化测试。知道他们有saas版可以试用,果断使用了一下,对其中场景管理和性能测试印象深刻,之后也在公司自己安装使用,接下来和大家介绍一下RunnerGo的整体使用情况。 Ru......
  • Windows 下的 iCloud 千万慎用!
    Windows下的iCloud总是同步被挂起,今天稀里糊涂的注销了账号想重新弄。结果一注销,所有尚未同步的文件一起丢失了,损失惨重。而且,就算是注销以后重新登录,依旧无法完成同步,服气了。目前的解决方法是卸载微软商店版本的iCloud,独立安装上一个老版本的14.1,目前同步非常丝滑,丢失的......
  • go 简单的CRUD
    packagemainimport( "database/sql" "fmt" _"github.com/mattn/go-sqlite3" "log" "time")/**idintprimarykeynotnull,nametextnotnull,ageintnotnull,......
  • apache.poi设置word页眉页脚
    XWPFDocumentdocx=newXWPFDocument();XWPFHeaderFooterPolicyheaderFooterPolicy=docx.getHeaderFooterPolicy();if(headerFooterPolicy==null)headerFooterPolicy=docx.createHeaderFooterPolicy();String......
  • C#搜索注册表
    stringkeyPath=@"Software\***\***";stringvalueName="ValueName";using(Microsoft.Win32.RegistryKeykey=Microsoft.Win32.Registry.CurrentUser.OpenSubKey(keyPath)){......
  • PEM 和 PKCS12
    都是公钥会或者私钥的格式PEM格式是一种基于文本的文件格式,通常包含使用Base64编码的证书、私钥或公钥。PEM文件通常以.pem或.crt扩展名结尾。PEM文件可以包含单个证书或密钥,也可以是多个证书或密钥的集合。PKCS12格式是一种二进制格式,通常包含一组证书及其相应......
  • 周藤 CSP-2023游记
    Day-inf~Day-2基本上是考试状态,每天我都是自己取随机题目做,不过也保证了落实量每场模拟赛发挥基本上是不是特别稳定,考得好的时候AK了,考不好的时候只有300分,反正同届差不多第一吧。。。不过还被几个人诅咒爆零了,不过没事,一交解千愁/seDay-1教练说了考试注意事项,然后就去娱......
  • CodeForces 1246F Cursor Distance
    洛谷传送门CF传送门发现一个性质:能跳不超过\(j\)步到达\(i\)的所有点形成一段区间。设这这段区间为\([L_{i,j},R_{i,j}]\)。那么答案即为:\[\sum\limits_{i=1}^n\sum\limits_{j=0}n-R_{i,j}+L_{i,j}-1\]并且:\[[L_{i,j},R_{i,j}]=\bigcup\limits_......
  • var、let 和 const 有什么区别
    var、let和const有什么区别 在本文中,我将带你深入了解JavaScript中的三个变量声明关键字:var、let和const。这些关键字在声明变量时起着不同的作用,我们将逐个探讨它们的区别,并给出一些实例来帮助加深理解。1.var:传统的变量声明方式在ES5及之前的JavaScrip......
  • 设计模式-工厂方法(Factory Method)
    1.定义 定义一个创建对象的接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行。2.示例创建一个接口:publicinterfaceShape{voiddraw();}创建接口具体实现类 publicclassSquareimplementsShape{@Overridepublicvoiddraw(){......