• LINQ的内部联接、分组联接和左外部联接


    最近在优化定时任务相关的代码,建议是把总查询放到内存中去坐,尽量减少打开的数据库连接

    1. 内连接

    指的是结果生成两张表可以连接的部分

    1. private void button1_Click_1(object sender, EventArgs e)
    2. {
    3. //初始化Student数组
    4. Student[] arrStu = new Student[]{
    5. new Student{ID=1,SName="zhangsan",Age=20},
    6. new Student{ID=2,SName="lisi",Age=21},
    7. new Student{ID=3,SName="wangwu",Age=23},
    8. new Student{ID=4,SName="liuliu",Age=24},
    9. };
    10. //初始化Product数组
    11. Product[] arrPro = new Product[]{
    12. new Product{ID=1,PName="Apple",Price=2.25},
    13. new Product{ID=2,PName="Orange",Price=5.25},
    14. new Product{ID=3,PName="Banana",Price=7.5},
    15. new Product{ID=4,PName="StrawBerry",Price=6.5},
    16. };
    17. //标准查询运算符
    18. var query = arrStu.Join(arrPro, s => s.ID, p => p.ID, (s, p) =>new { s.SName, s.Age, p.PName, p.Price });
    19. StringBuilder sbRes = new StringBuilder();
    20. //打印
    21. foreach (var item in query)
    22. {
    23. sbRes.AppendFormat("SName:{0},Age:{1},PName:{2},Price:{3}", item.SName, item.Age, item.PName, item.Price);
    24. sbRes.AppendLine();
    25. }
    26. MessageBox.Show(sbRes.ToString());
    27. }

    或者另外一种写法

    1. //LINQ语句
    2. var query = from sItem in arrStu
    3. join pItem in arrPro
    4. on sItem.ID equals pItem.ID
    5. select new { sItem.SName, sItem.Age, pItem.PName, pItem.Price };//Select后面接一个匿名对象

    2.  分组连接

    第一个集合中的每个元素与第二个集合中的一组相关元素进行配对

    使用 into关键词

    1. Person magnus = new(FirstName: "Magnus", LastName: "Hedlund");
    2. Person terry = new("Terry", "Adams");
    3. Person charlotte = new("Charlotte", "Weiss");
    4. Person arlene = new("Arlene", "Huff");
    5. List people = new() { magnus, terry, charlotte, arlene };
    6. List pets = new()
    7. {
    8. new(Name: "Barley", Owner: terry),
    9. new("Boots", terry),
    10. new("Whiskers", charlotte),
    11. new("Blue Moon", terry),
    12. new("Daisy", magnus),
    13. };
    14. // Create a list where each element is an anonymous type
    15. // that contains the person's first name and a collection of
    16. // pets that are owned by them.
    17. var query =
    18. from person in people
    19. join pet in pets on person equals pet.Owner into gj
    20. select new
    21. {
    22. OwnerName = person.FirstName,
    23. Pets = gj
    24. };
    25. foreach (var v in query)
    26. {
    27. // Output the owner's name.
    28. Console.WriteLine($"{v.OwnerName}:");
    29. // Output each of the owner's pet's names.
    30. foreach (var pet in v.Pets)
    31. {
    32. Console.WriteLine($" {pet.Name}");
    33. }
    34. }
    35. /* Output:
    36. Magnus:
    37. Daisy
    38. Terry:
    39. Barley
    40. Boots
    41. Blue Moon
    42. Charlotte:
    43. Whiskers
    44. Arlene:
    45. */

    3. 左外连接

    简单理解就是,基于第一张表,无论第二章表是否有匹配项,都放进去

    左连接的数据 一定 大于等于 内连接

    1. Person magnus = new("Magnus", "Hedlund");
    2. Person terry = new("Terry", "Adams");
    3. Person charlotte = new("Charlotte", "Weiss");
    4. Person arlene = new("Arlene", "Huff");
    5. Pet barley = new("Barley", terry);
    6. Pet boots = new("Boots", terry);
    7. Pet whiskers = new("Whiskers", charlotte);
    8. Pet bluemoon = new("Blue Moon", terry);
    9. Pet daisy = new("Daisy", magnus);
    10. // Create two lists.
    11. List people = new() { magnus, terry, charlotte, arlene };
    12. List pets = new() { barley, boots, whiskers, bluemoon, daisy };
    13. var query =
    14. from person in people
    15. join pet in pets on person equals pet.Owner into gj
    16. from subpet in gj.DefaultIfEmpty()
    17. select new
    18. {
    19. person.FirstName,
    20. PetName = subpet?.Name ?? string.Empty
    21. };
    22. foreach (var v in query)
    23. {
    24. Console.WriteLine($"{v.FirstName + ":",-15}{v.PetName}");
    25. }
    26. record class Person(string FirstName, string LastName);
    27. record class Pet(string Name, Person Owner);
    28. // This code produces the following output:
    29. //
    30. // Magnus: Daisy
    31. // Terry: Barley
    32. // Terry: Boots
    33. // Terry: Blue Moon
    34. // Charlotte: Whiskers
    35. // Arlene:

    reference:

    1. 执行左外部联接(C# 中的 LINQ) - C# | Microsoft Learn

  • 相关阅读:
    【技术积累】Vue.js中的核心知识【二】
    【数据库系统概论】第四章数据库安全性
    Vue3较Vue2的更新内容(一)
    springboot自定义全局异常,非空等参数校验异常
    DSP、DMP、CDP、CRM
    开发板TFTP调试
    google的chromedriver最新版下载地址
    [PAT练级笔记] 09 Basic Level 1011
    day52--动态规划11
    YOLOv8-pose关键点检测:模型轻量化创新 |轻量级可重参化EfficientRepBiPAN
  • 原文地址:https://blog.csdn.net/dongnihao/article/details/132891985