LinqJoin方法
Linq知识点总结:
(一)、构建两个List泛型集合
List<Person> list=new List<Person>()
{
new Person(){ Name="Olive",Sex="女",age=22},
new Person(){ Name="Moyao",Sex="男",age=23},
new Person(){ Name="Momo",Sex="女",age=22},
new Person(){ Name="Only",Sex="女",age=20},
new Person(){ Name="Love",Sex="女",age=21},
new Person(){ Name="For",Sex="女",age=22},
new Person(){ Name="Remote",Sex="男",age=23},
new Person(){ Name="Snow",Sex="女",age=23}
};
List<Profession> listprofession = new List<Profession>
{
new Profession() { Name = "Olive", Zhiye = "会计" },
new Profession() { Name = "Remote", Zhiye = "IT Coder" },
new Profession() { Name = "BLove", Zhiye = "学生" },
new Profession(){ Name="AFor",Zhiye="作家"}
};
(二)、使用Linq中的Join方法
var showzhiye = from p in list
join pf in listprofession on p.Name equals pf.Name
select new
{
Name = p.Name,
ZhiYe = pf.Zhiye
};
(三)、控制台输出
foreach(var z in showzhiye)
{
Console.WriteLine("姓名:" + z.Name + "职业:" + z.ZhiYe);
}
Console.WriteLine("使用Join()方法");
var showzhiye2 = list.Join(listprofession, p => p.Name, pf => pf.Name, (p, pf) => new
{
Name = p.Name,
Zhiye = pf.Zhiye
});
foreach (var z in showzhiye2)
{
Console.WriteLine("姓名:" + z.Name + "职业:" + z.Zhiye);
}