Linq中eft join之大坑

  1 using System;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using System.Data;
  5 using System.IO;
  6 using System.Linq;
  7 using Newtonsoft.Json;
  8
  9 namespace CLibrary.ConsoleApp
 10 {
 11     class Program
 12     {
 13         static void Main(string[] args)
 14         {
 15             var tableAAA = "[{\"companycode\":\"80463417\",\"securitycode\":\"603978\",\"securityshortname\":null,\"financecode\":null,\"purchasedate\":\"2017-07-26T00:00:00\",\"listingdate\":\"2017-08-07T00:00:00\",\"issueprice\":29.93}]";
 16             var tableBBB = "[{\"securityvarietycode\":\"1000576786\",\"companycode\":\"80463417\",\"securitycode\":\"603978\",\"shares\":1000}]";
 17             var tableTTT = "[{\"securityvarietycode\":\"1000576786\",\"srkpj\":35.92,\"srspj\":43.1,\"srzdf\":44.0027,\"srhsl\":0.06}]";
 18             var tableEEE = "[{\"secucode\":\"603978\",\"tdate\":\"2017-08-07T00:00:00\",\"high\":43.1},{\"secucode\":\"603978\",\"tdate\":\"2017-08-08T00:00:00\",\"high\":47.41}]";
 19             var tableMMM = "[]";
 20             var tableJJJ = "[{\"secucode\":\"603978\",\"tdate\":\"2017-08-07T00:00:00\",\"avgprice\":42.82},{\"secucode\":\"603978\",\"tdate\":\"2017-08-08T00:00:00\",\"avgprice\":47.41}]";
 21             var tableNNN = "[{\"secucode\":\"603978\",\"tdate\":\"2017-08-07T00:00:00\",\"high\":43.1},{\"secucode\":\"603978\",\"tdate\":\"2017-08-08T00:00:00\",\"high\":47.41}]";
 22
 23             var tableA = JsonConvert.DeserializeObject<List<TableA>>(tableAAA);
 24             var tableB = JsonConvert.DeserializeObject<List<TableB>>(tableBBB);
 25             var tableT = JsonConvert.DeserializeObject<List<TableT>>(tableTTT);
 26             var tableE = JsonConvert.DeserializeObject<List<TableE>>(tableEEE);
 27             var tableM = JsonConvert.DeserializeObject<List<TableM>>(tableMMM);
 28             var tableJ = JsonConvert.DeserializeObject<List<TableJ>>(tableJJJ);
 29             var tableN = JsonConvert.DeserializeObject<List<TableE>>(tableNNN);
 30
 31             var query = from a in tableA
 32                         join b in tableB on a.companycode equals b.companycode into ab
 33                         from def_b in ab.DefaultIfEmpty(new TableB())
 34                         join t in tableT on def_b.securityvarietycode equals t.securityvarietycode into bt
 35                         join e in tableE on a.listingdate equals e.tdate into ae
 36                         join m in tableM on a.securitycode equals m.securitycode into am
 37                         from def_m in am.DefaultIfEmpty(new TableM())
 38                         join j in tableJ on def_m.tdatep equals j.tdate into mj
 39                         join n in tableN on def_m.tdatep equals n.tdate into mn
 40                         from def_t in bt.DefaultIfEmpty(new TableT())
 41                         from def_e in ae.DefaultIfEmpty(new TableE())
 42                         from def_j in mj.DefaultIfEmpty(new TableJ())
 43                         orderby def_m.tdatep
 44                         select new Result
 45                         {
 46                             listingopen = def_t.srkpj,
 47                             listingclose = def_t.srspj,
 48                             listingopenpremium = Math.Round((def_t.srkpj / a.issueprice - 1) * 100, 2),
 49                             listingchg = def_t.srzdf,
 50                             listingturnover = def_t.srhsl,
 51                             listinghighpchg = Math.Round((def_e.high / a.issueprice - 1) * 100, 2),
 52                             opendate = def_m.tdatep,
 53                             highpchg = 0d, //api层处理,
 54                             limitupdays = def_m.days,
 55                             listingavg = def_j.avgprice,
 56                             profit = (def_j.avgprice - a.issueprice) * def_b.shares,//api层处理,
 57                             issuePrice = a.issueprice,//用于api层处理
 58                             shares = def_b.shares,//用于api层处理
 59                         };
 60
 61             var list = query.ToList();
 62
 63             Console.WriteLine(JsonConvert.SerializeObject(list));
 64             Console.ReadKey();
 65         }
 66
 67         #region Class
 68         private class TableA
 69         {
 70             public string companycode { get; set; }
 71             public string securitycode { get; set; }
 72             public string securityshortname { get; set; }
 73             public string financecode { get; set; }
 74             public DateTime purchasedate { get; set; }
 75             public DateTime listingdate { get; set; }
 76             public double issueprice { get; set; }
 77         }
 78         private class TableB
 79         {
 80             public string securityvarietycode { get; set; }
 81             public string companycode { get; set; }
 82             public string securitycode { get; set; }
 83             public int shares { get; set; }
 84         }
 85
 86         private class TableE
 87         {
 88             public string secucode { get; set; }
 89             public DateTime tdate { get; set; }
 90             public double high { get; set; }
 91         }
 92         private class TableJ
 93         {
 94             public string secucode { get; set; }
 95             public DateTime tdate { get; set; }
 96             public double avgprice { get; set; }
 97         }
 98         private class TableM
 99         {
100             public string securitycode { get; set; }
101             public DateTime tdatep { get; set; }
102             public int days { get; set; }
103         }
104         private class TableT
105         {
106             public string securityvarietycode { get; set; }
107             public double srkpj { get; set; }
108             public double srspj { get; set; }
109             public double srzdf { get; set; }
110             public double srhsl { get; set; }
111         }
112         private class Result
113         {
114             public double listingopen { get; set; }
115             public double listingclose { get; set; }
116             public double listingopenpremium { get; set; }
117             public double listingchg { get; set; }
118             public double listingturnover { get; set; }
119             public double listinghighpchg { get; set; }
120             public DateTime opendate { get; set; }
121             public double highpchg { get; set; }
122             public int limitupdays { get; set; }
123             public double listingavg { get; set; }
124             public double profit { get; set; }
125             public double issuePrice { get; set; }
126             public int shares { get; set; }
127         }
128
129         #endregion
130
131     }
132
133 }
时间: 2024-12-17 04:24:57

Linq中eft join之大坑的相关文章

数据库和linq中的 join(连接)操作

sql中的连接 sql中的表连接有inner join,left join(left outer join),right join(right outer join),full join(full outer join),cross join 在此基础上我们能扩展出 left excluding join,right excluding join,full outer excluding join 注:left join是left outer join 的简写,即左连接和左外连接是一样的 首先定

LINQ TO SQL 中的join(转帖)

http://www.cnblogs.com/ASPNET2008/archive/2008/12/21/1358152.html join对于喜欢写SQL的朋友来说还是比较实用,也比较容易接受的东西.在LINQ TO SQL中,写多表查询,同样可以写join,只是它有它自己的语法要求而已,语义都是一样的,下面我来讲下LINQ TO SQL中的join最基本的形式:都是最简单的,当然还有其它方面的内容,如:怎样加上过滤条件,如何分组,如何排序等等,为了单纯说join的用法,这里就简化下. Cod

LINQ中的连接(join)用法示例

Linq中连接主要有组连接.内连接.左外连接.交叉连接四种.各个用法如下. 1. 组连接 组连接是与分组查询是一样的.即根据分组得到结果. 如下例,根据publisther分组得到结果. 使用组连接的查询语句如下: //使用组连接 var GroupQuery = from publisher in SampleData.Publishers join book in SampleData.Books on publisher equals book.Publisher into publish

Linq中的连接(join)

Linq中连接主要有组连接.内连接.左外连接.交叉连接四种.各个用法如下. 注:本文内容主要来自<Linq实战>,本例中用到的对象请见文章底部. 1. 组连接 组连接是与分组查询是一样的.即根据分组得到结果. 如下例,根据publisther分组得到结果. 使用组连接的查询语句如下: //使用组连接            var GroupQuery = from publisher in SampleData.Publishers                             jo

Linq中join &amp; group join &amp; left join 的用法

Linq中join & group join & left join 的用法 2013-01-30 11:12 12154人阅读 评论(0) 收藏 举报  分类: C#(14)  文章转自:http://www.cnblogs.com/c-jquery-linq-sql-net-problem/archive/2011/01/17/LINQ_Inner_Join_Group_Join_Left_Join.html 我们在做SQL查询的时候经常会用到Inner Join,Left Join,

Linq 中 Join 的用法

Linq中连接主要有组连接.内连接.左外连接.交叉连接四种.各个用法如下. 注:本文内容主要来自<Linq实战>,本例中用到的对象请见文章底部. 1. 组连接 组连接是与分组查询是一样的.即根据分组得到结果. 如下例,根据publisther分组得到结果. 使用组连接的查询语句如下: //使用组连接            var GroupQuery = from publisher in SampleData.Publishers                             jo

Linq中使用Left Join

use Test Create table Student( ID int identity(1,1) primary key, [Name] nvarchar(50) not null ) Create Table Book( ID int identity(1,1) primary key, [Name] nvarchar(50)not null, StudentID int not null ) insert into Student values('张三') insert into St

LINQ中的一些查询语句格式

LINQ的基本格式如下所示:var <变量> = from <项目> in <数据源> where <表达式> orderby <表达式> LINQ 基本子句from查询子句——基础后面跟随着项目名称和数据源示例代码如下:var str = from lq in str select lq; 其中select语句指定了返回到集合变量中的元素是来自哪个数据源的 from查询子句——嵌套查询可以在from子句中嵌套另一个from子句即可,示例代码如下

LINQ:开始使用 LINQ(五)- LINQ 中的查询语法和方法语法

开始使用 LINQ(五)- LINQ 中的查询语法和方法语法 在表示语言集成查询 (LINQ) 使用 LINQ 性查询语法,文档中的多数查询编写.但是,编译代码时,必须将查询语法转换为方法,这就需要 .NET 公共语言运行时 (CLR).这些方法调用标准查询运算符的名称类似 Where.Select.GroupBy.Join.Max和 Average.可以调用这些方法直接使用方法语法而不是查询语法. 查询语法和方法语法语义相同,但是,许多人员发现查询语法更简单.更易于阅读.某些查询必须表示为方法