C#中IEnumerableT.Join()和IEnumerableT.GroupJoin()简单使用
生活随笔
收集整理的這篇文章主要介紹了
C#中IEnumerableT.Join()和IEnumerableT.GroupJoin()简单使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
背景介紹:
在無主外鍵關系的表中如果如果要關聯就要用Join()和GroupJoin()方法了,我們先看Join()方法,代碼如下:
static void Main(string[] args) {List<SW_XSDD> sw_xsdd = new List<SW_XSDD>(){new SW_XSDD { com_id = "SH01",vou_no = "100",corr_id = "A",corr_date = DateTime.Now},new SW_XSDD { com_id = "SH02",vou_no = "200",corr_id = "B",corr_date = DateTime.Now.AddDays(1)},new SW_XSDD { com_id = "SH03",vou_no = "300",corr_id = "C",corr_date = DateTime.Now.AddDays(2)}};List<SW_XSDD_C> sw_xsdd_c = new List<SW_XSDD_C>(){new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700001",number = 10,vou_line_no = 1},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700002",number = 11,vou_line_no = 2},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700003",number = 12,vou_line_no = 3},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700004",number = 13,vou_line_no = 4},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700005",number = 14,vou_line_no = 5},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700006",number = 15,vou_line_no = 6},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700007",number = 16,vou_line_no = 1},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700008",number = 17,vou_line_no = 2},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700009",number = 18,vou_line_no = 3},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700010",number = 19,vou_line_no = 4},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700011",number = 20,vou_line_no = 5},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700012",number = 21,vou_line_no = 6},};var Lists = sw_xsdd.Join(sw_xsdd_c, p => new { com_id = p.com_id, vou_no = p.vou_no }, q => new { com_id = q.com_id, vou_no = q.vou_no }, (p, q) => new { com_id = p.com_id, vou_no = p.vou_no, vou_line_no = q.vou_line_no, corr_id = p.corr_id, corr_date = p.corr_date, item_no = q.item_no, number = q.number });foreach (var list in Lists){Console.WriteLine($"{list.com_id}-{list.vou_no}-{list.vou_line_no}-{list.item_no}-{list.number}-{list.corr_id}-{list.corr_date}");}Console.Read(); }public class SW_XSDD {public string com_id { get; set; }public string vou_no { get; set; }public string corr_id { get; set; }public DateTime corr_date { get; set; } }public class SW_XSDD_C {public string com_id { get; set; }public string vou_no { get; set; }public int vou_line_no { get; set; }public string item_no { get; set; }public int number { get; set; } }上述代碼中SW_XSDD實體類和SW_XSDD_C實體類通過 com_id和vou_no 這兩個字段關聯,在SW_XSDD中存在一條com_id為SH03的數據,這條數據在SW_XSDD_C中因為沒有對應數據是無法關聯到的,運行結果如下所示:
從運行結果中得知,Join()方法應該是SQL中INNER JOIN,SW_XSDD_C中沒有的數據就不顯示了。
下面再看GroupJoin()方法,代碼如下:
public class SW_XSDD {public string com_id { get; set; }public string vou_no { get; set; }public string corr_id { get; set; }public DateTime corr_date { get; set; } }public class SW_XSDD_C {public string com_id { get; set; }public string vou_no { get; set; }public int vou_line_no { get; set; }public string item_no { get; set; }public int number { get; set; } } static void Main(string[] args) {List<SW_XSDD> sw_xsdd = new List<SW_XSDD>(){new SW_XSDD { com_id = "SH01",vou_no = "100",corr_id = "A",corr_date = DateTime.Now},new SW_XSDD { com_id = "SH02",vou_no = "200",corr_id = "B",corr_date = DateTime.Now.AddDays(1)},new SW_XSDD { com_id = "SH03",vou_no = "300",corr_id = "C",corr_date = DateTime.Now.AddDays(2)}};List<SW_XSDD_C> sw_xsdd_c = new List<SW_XSDD_C>(){new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700001",number = 10,vou_line_no = 1},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700002",number = 11,vou_line_no = 2},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700003",number = 12,vou_line_no = 3},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700004",number = 13,vou_line_no = 4},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700005",number = 14,vou_line_no = 5},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700006",number = 15,vou_line_no = 6},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700007",number = 16,vou_line_no = 1},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700008",number = 17,vou_line_no = 2},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700009",number = 18,vou_line_no = 3},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700010",number = 19,vou_line_no = 4},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700011",number = 20,vou_line_no = 5},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700012",number = 21,vou_line_no = 6},};var Lists = sw_xsdd.GroupJoin(sw_xsdd_c, p => new { com_id = p.com_id, vou_no = p.vou_no }, q => new { com_id = q.com_id, vou_no = q.vou_no }, (p, k) => new { com_id = p.com_id, vou_no = p.vou_no,corr_id = p.corr_id, corr_date = p.corr_date,rest = k });foreach (var list in Lists){Console.WriteLine($"{list.com_id}-{list.vou_no}-{list.corr_id}-{list.corr_date}");foreach (var l in list.rest){Console.WriteLine($"{l.vou_line_no}-{l.item_no}-{l.number}");}}Console.Read(); }在這句代碼中:
var Lists = sw_xsdd.GroupJoin(sw_xsdd_c, p => new { com_id = p.com_id, vou_no = p.vou_no }, q => new { com_id = q.com_id, vou_no = q.vou_no }, (p, k) => new { com_id = p.com_id, vou_no = p.vou_no,corr_id = p.corr_id, corr_date = p.corr_date,rest = k });K為類型為:,所以無法直接取出SW_XSDD_C中特有的字段,需要用到雙重循環實現,運行結果如下所示:
可以看到SW_XSDD中com_id為SH03的數據顯示出來了,只是沒有SW_XSDD_C中的數據,類似于SQL 中的 LEFT JOIN。
總結
以上是生活随笔為你收集整理的C#中IEnumerableT.Join()和IEnumerableT.GroupJoin()简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IP地址 子网掩码 网关
- 下一篇: JavaWeb关于工程运行的笔记