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 ...
随机推荐
- UUID+随机数
import java.util.Random; import java.util.UUID; public class UUIDUtils { public static String getUUI ...
- DevExpress ASP.NET v18.2新功能详解(四)
行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExpress ASP.NET Cont ...
- SharePoint online Multilingual support - Development(1)
博客地址:http://blog.csdn.net/FoxDave 上一节讲了SharePoint Online网站多语言的实现原理机制,本节主要从编程的角度来谈一下如何进行相关的设置. 下面列出 ...
- <Spark><Programming><RDDs>
Introduction to Core Spark Concepts driver program: 在集群上启动一系列的并行操作 包含应用的main函数,定义集群上的分布式数据集,操作数据集 通过 ...
- synchronized(三)
package com.bjsxt.base.sync003; /** * 对象锁的同步和异步问题 * @author alienware * */public class MyObject { pu ...
- ChinaCock界面控件介绍-CCNewsSilder
上图是控件包里的Demo运行效果,轮播新闻图片. 这个控件用起来简单,拖放一个CCNewsSiler到Form上,设置Align为Top,再设置好高度,然后用代码加载图片: procedure TFo ...
- IONIC和Cordova安装、打包踩过的坑
1.问题1:直接执行npm install -g cordova ionic,因为网络原因,执行不成功 解决方案:将npm映射到淘宝服务器:npm install -g cnpm --registry ...
- Python 封装
# 封装: # 1. 对属性的封装 # 2. 对功能的封装 # 3. 模块 # 4. 包 class Student: def __init__(self, num, name, clazz): se ...
- SQL注入之Sqli-labs系列第十二关
开始挑战第十二关(Error Based- Double quotes- String) 12点半了,不困,继续,继续,继续 先看看页面,通常的使用单引号等进行操作,看看啥么情况先 咦,出现错误信息了 ...
- [转]谈谈 Bias-Variance Tradeoff
https://liam0205.me/2017/03/25/bias-variance-tradeoff/ 谢谢原作者! 谈谈 Bias-Variance Tradeoff 发表于 2017 年 0 ...