Linq中left 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 }