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 ...
随机推荐
- Android开发 --代码布局
Android开发 --代码布局 在线性布局LinearLayout里加入view比较简单,因为属性比较少,布局简单 示例,加入一个TextView LinearLayout layout = (Li ...
- easyui datagrid 首次不加载做法
我们一般遇到首次不执行查询,只有你点击查询按钮才查询的功能 我使用easyui的datagird做法是这样的: onBeforeLoad: function (param) { var firstLo ...
- jdk1.8使用的url和driverName的改变
#dataSource configure connection.url=jdbc:mysql://localhost:3306/shiro?useSSL=false&serverTimezo ...
- Redis部署与基本操作
1.安装 1)不指定安装位置,则会把redis的可执行文件安装到 redis-2.8.6/src/目录下 [root@CentOS6 ~]# ls anaconda-ks.cfg httpd-2. ...
- Docker(1):初体验之应用挂载到容器
需在安装docker的机器上运行,本文机器环境为Win10,目录可根据实际自行修改. 1.首先创建一个目录:D:\docker\ROOT\WEB-INF 2.在D:\docker\ROOT\WEB-I ...
- 设置table中的td一连串内容自动换行
遇到一长串字母撑出了td宽度,导致整个表格错乱,如图: , 解决办法: 第一: table 加上css: table-layout: fixed;(此css属性 表示 列宽由表格宽度和列宽度设定.不会 ...
- 《RECURRENT BATCH NORMALIZATION》
原文链接 https://arxiv.org/pdf/1603.09025.pdf Covariate 协变量:在实验的设计中,协变量是一个独立变量(解释变量),不为实验者所操纵,但仍影响实验结果. ...
- 2019.3.5 L261 Are All Our Organs Vital?
Medicine has not always shown a lot of respect for the human body. Just think about the ghoulish dis ...
- Python 关联关系
class Boy: def __init__(self, name, girlFriend=None): # 在初始化的时候可以给一个对象的属性设置成另一个类的对象 self.girlFriend ...
- python的ConfigParser模块
前言 处理配置文件做增.删.改.查 操作. 配置文件的格式如下:“[ ]”包含的为 section,section 下面为类似于 key - value 的配置内容: configparser 默认支 ...