代码
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp1 { class Program { static void Main(string[] args) { List<Pet> pets = new List<Pet>{ new Pet { Name="A", Age=1 }, new Pet { Name="B", Age= 2}, new Pet { Name="C", Age=3 } }; List<Pet2> pets2 = new List<Pet2>{ new Pet2 { Name="A", Sex = "男" }, new Pet2 { Name="A", Sex = "男"}, new Pet2 { Name="B", Sex = "男" }, }; var query = from p1 in pets join pet2 in pets2 on p1.Name equals pet2.Name select new { p1, pet2 }; var list = query.ToList(); Console.WriteLine(list.Count); foreach (var item in list) { Console.WriteLine($"pet={item.p1.Name},{item.p1.Age} pet2={item.pet2.Name},{item.pet2.Sex}"); } Console.WriteLine("Hello World!"); } } class Pet { public string Name { get; set; } public int Age { get; set; } } class Pet2 { public string Name { get; set; } public string Sex { get; set; } } }
原文:https://www.cnblogs.com/wangyinlon/p/13387236.html