Linq中left join之多表查询
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using Newtonsoft.Json; namespace CLibrary.ConsoleApp
{
class Program
{
static void Main(string[] args)
{
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}]";
var tableBBB = "[{\"securityvarietycode\":\"1000576786\",\"companycode\":\"80463417\",\"securitycode\":\"603978\",\"shares\":1000}]";
var tableTTT = "[{\"securityvarietycode\":\"1000576786\",\"srkpj\":35.92,\"srspj\":43.1,\"srzdf\":44.0027,\"srhsl\":0.06}]";
var tableEEE = "[{\"secucode\":\"603978\",\"tdate\":\"2017-08-07T00:00:00\",\"high\":43.1},{\"secucode\":\"603978\",\"tdate\":\"2017-08-08T00:00:00\",\"high\":47.41}]";
var tableMMM = "[]";
var tableJJJ = "[{\"secucode\":\"603978\",\"tdate\":\"2017-08-07T00:00:00\",\"avgprice\":42.82},{\"secucode\":\"603978\",\"tdate\":\"2017-08-08T00:00:00\",\"avgprice\":47.41}]";
var tableNNN = "[{\"secucode\":\"603978\",\"tdate\":\"2017-08-07T00:00:00\",\"high\":43.1},{\"secucode\":\"603978\",\"tdate\":\"2017-08-08T00:00:00\",\"high\":47.41}]"; var tableA = JsonConvert.DeserializeObject<List<TableA>>(tableAAA);
var tableB = JsonConvert.DeserializeObject<List<TableB>>(tableBBB);
var tableT = JsonConvert.DeserializeObject<List<TableT>>(tableTTT);
var tableE = JsonConvert.DeserializeObject<List<TableE>>(tableEEE);
var tableM = JsonConvert.DeserializeObject<List<TableM>>(tableMMM);
var tableJ = JsonConvert.DeserializeObject<List<TableJ>>(tableJJJ);
var tableN = JsonConvert.DeserializeObject<List<TableE>>(tableNNN); var query = from a in tableA
join b in tableB on a.companycode equals b.companycode into ab
from def_b in ab.DefaultIfEmpty(new TableB())
join t in tableT on def_b.securityvarietycode equals t.securityvarietycode into bt
join e in tableE on a.listingdate equals e.tdate into ae
join m in tableM on a.securitycode equals m.securitycode into am
from def_m in am.DefaultIfEmpty(new TableM())
join j in tableJ on def_m.tdatep equals j.tdate into mj
join n in tableN on def_m.tdatep equals n.tdate into mn
from def_t in bt.DefaultIfEmpty(new TableT())
from def_e in ae.DefaultIfEmpty(new TableE())
from def_j in mj.DefaultIfEmpty(new TableJ())
orderby def_m.tdatep
select new Result
{
listingopen = def_t.srkpj,
listingclose = def_t.srspj,
listingopenpremium = Math.Round((def_t.srkpj / a.issueprice - ) * , ),
listingchg = def_t.srzdf,
listingturnover = def_t.srhsl,
listinghighpchg = Math.Round((def_e.high / a.issueprice - ) * , ),
opendate = def_m.tdatep,
highpchg = 0d, //api层处理,
limitupdays = def_m.days,
listingavg = def_j.avgprice,
profit = (def_j.avgprice - a.issueprice) * def_b.shares,//api层处理,
issuePrice = a.issueprice,//用于api层处理
shares = def_b.shares,//用于api层处理
}; var list = query.ToList(); Console.WriteLine(JsonConvert.SerializeObject(list));
Console.ReadKey();
} #region Class
private class TableA
{
public string companycode { get; set; }
public string securitycode { get; set; }
public string securityshortname { get; set; }
public string financecode { get; set; }
public DateTime purchasedate { get; set; }
public DateTime listingdate { get; set; }
public double issueprice { get; set; }
}
private class TableB
{
public string securityvarietycode { get; set; }
public string companycode { get; set; }
public string securitycode { get; set; }
public int shares { get; set; }
} private class TableE
{
public string secucode { get; set; }
public DateTime tdate { get; set; }
public double high { get; set; }
}
private class TableJ
{
public string secucode { get; set; }
public DateTime tdate { get; set; }
public double avgprice { get; set; }
}
private class TableM
{
public string securitycode { get; set; }
public DateTime tdatep { get; set; }
public int days { get; set; }
}
private class TableT
{
public string securityvarietycode { get; set; }
public double srkpj { get; set; }
public double srspj { get; set; }
public double srzdf { get; set; }
public double srhsl { get; set; }
}
private class Result
{
public double listingopen { get; set; }
public double listingclose { get; set; }
public double listingopenpremium { get; set; }
public double listingchg { get; set; }
public double listingturnover { get; set; }
public double listinghighpchg { get; set; }
public DateTime opendate { get; set; }
public double highpchg { get; set; }
public int limitupdays { get; set; }
public double listingavg { get; set; }
public double profit { get; set; }
public double issuePrice { get; set; }
public int shares { get; set; }
} #endregion } }
Linq中left join之多表查询的更多相关文章
- springboot中使用JOIN实现关联表查询
* 首先要确保你的表和想要关联的表有外键连接 repository中添加接口JpaSpecificationExecutor<?>,就可以使用springboot jpa 提供的API了. ...
- 数据库和linq中的 join(连接)操作
sql中的连接 sql中的表连接有inner join,left join(left outer join),right join(right outer join),full join(full o ...
- 利用EF Core的Join进行多表查询
背景 话说有这么一家子,老公养了一条狗,老婆养了一只猫. 数据库的设计 人表 宠物表 通过表可以知道,宠物通过Owner指向主人的Id. 问题来了,我要和故事开头一样,老公-狗,老婆-猫,对应起来,怎 ...
- thinkphp中如何是实现多表查询
多表查询经常使用到,但如何在thinkphp中实现多表查询呢,其实有三种方法. 1 2 3 4 5 6 7 8 9 10 11 12 // 1.原生查询示例: $Model = new Model() ...
- 在mybatis框架中,延迟加载与连表查询的差异
1.引子 mybatis的延迟加载,主要应用于一个实体类中有复杂数据类型的属性,包括一对一和一对多的关系(在xml中用collection.association标签标识).这个种属性往往还对应着另一 ...
- MySQL select join on 连表查询和自连接查询
连表查询 JOIN ON 操作 描述 inner join 只返回匹配的值 right join 会从右表中返回所有的值, 即使左表中没有匹配 left join 会从左表中返回所有的值, 即使右表中 ...
- MySQL中 如何查询表名中包含某字段的表 ,查询MySql数据库架构信息:数据库,表,表字段
--查询tablename 数据库中 以"_copy" 结尾的表 select table_name from information_schema.tables where ta ...
- EntityFramework 使用Linq处理内连接(inner join)、外链接(left/right outer join)、多表查询
场景:在实际的项目中使用EntityFramework都会遇到使用Ef处理连接查询的问题,这里做一些小例子如何通过Linq语法处理内连接(inner join).外连接(left/right oute ...
- Linq中的group by多表多字段
在sql中,如果有group by,那么select的字段只能包含分组内容,或者count.sum.avg这些统计字段. 但在linq里面,是:group 你想要什么字段 by 分组字段 比如: va ...
随机推荐
- [ZJOI2008]泡泡堂BNB
这个题...是一道神奇的贪心题... 根据田忌赛马的原理... 先假使两队都符合田忌和齐王的配置... 我们可以发现如果我们用当前最弱的...去和对方当前最强的打... 然后一直按照这个方案...当我 ...
- DevExpress ASP.NET Bootstrap Controls v18.2新功能详解(二)
行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExpress ASP.NET Boot ...
- DevExpress v18.1新版亮点——DevExtreme篇(四)
用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExtreme JavaScript Controls v18.1 的新功能 ...
- C Runtime Library、C Runtime
C Runtime Library.C Runtime 1)运行时库就是 C run-time library,是 C 而非 C++ 语言世界的概念:取这个名字就是因为你的 C 程序运行时需要这些 ...
- shell日常实战防dos攻击
根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100,即调用防火墙命令封掉对应的IP,监控频率每隔3分钟.防火墙命令为:iptables -I INPUT -s 10.0 ...
- jquery.datatables设置列隐藏的方法
项目需要根据权限设置表格(使用Juqery.datatables,版本:1.10.16)某列显示或隐藏,百度后有两种实现方法: 1.在columns中设置: columns:[{data:" ...
- JAVA_概念01_跨域
1.什么是跨域? 协议.域名.端口都相同是同域,否则是跨域. 服务器不允许ajax跨域获取数据 2.解决办法? ①jsonp :Jsonp不是一种数据格式,而json是一种数据格式,jsonp是用来解 ...
- 1.Windows下使用VisualSVN Server搭建SVN服务器
使用 VisualSVN Server来实现主要的 SVN功能则要比使用原始的 SVN和Apache相配合来实现源代码的 SVN管理简单的多,下面就看看详细的说明. VisualSVN Server的 ...
- sqlite 3基本使用方法
1.sqlite数据库数据类型 Integer 整型 varchar(10) 字符数组 float 浮点型 double 双精度浮点型 char(10) 字符型 text 文本型 2.sql语法 2. ...
- 2.19 cookie相关操作
2.19 cookie相关操作 前言虽然cookie相关操作在平常ui自动化中用得少,偶尔也会用到,比如登录有图形验证码,可以通过绕过验证码方式,添加cookie方法登录.登录后换账号登录时候,也可作 ...