[C#] LINQ之SelectMany和GroupJoin
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Person
{
public int CityID { set; get; }
public string Name { set; get; }
} class City
{
public int ID { set; get; }
public string CityName { set; get; }
} public class Program
{
public static void Main(string[] args)
{
Person[] persons = new Person[]
{
new Person {CityID = , Name = "ABC"},
new Person {CityID = , Name = "EFG"},
new Person {CityID = , Name = "HIJ"},
new Person {CityID = , Name = "KLM"},
new Person {CityID = , Name = "NOP"},
new Person {CityID = , Name = "QRS"},
new Person {CityID = , Name = "TUV"}
};
City[] cities = new City[]
{
new City {ID = , CityName = "Guangzhou"},
new City {ID = , CityName = "Shenzhen"},
new City {ID = , CityName = "Beijing"},
new City {ID = , CityName = "Shanghai"}
}; //例子1:内联
var result0 = from p in persons
join c in cities
on p.CityID equals c.ID
select new {PersonName = p.Name, Citys = c.CityName};
Console.Write("-----inner join linq--------");
Console.WriteLine();
foreach (var item in result0)
{
Console.Write(item.PersonName);
Console.Write(":");
Console.Write(item.Citys);
Console.WriteLine();
} Console.WriteLine(); //例子2:左联 linq ,这里使用了into,如果cities在persons没有匹配的项,则返回Citys是null,返回的Citys是list
var result01 = from p in persons
join c in cities on p.CityID equals c.ID into cs
select new { PersonName = p.Name, Citys = cs };
Console.Write("------left join linq list-------");
Console.WriteLine();
foreach (var item in result01)
{
Console.Write(item.PersonName);
Console.Write(":");
foreach (var city in item.Citys)
{
Console.Write(city.CityName);
}
Console.WriteLine();
} //例子3:左联 linq,这里使用了into,并且多了from t2 in cs.DefaultIfEmpty(),
//然后在返回的时候Citys进行了为null判断,不然result是没有内容的
var result = from p in persons
join c in cities
on p.CityID equals c.ID into cs
from t2 in cs.DefaultIfEmpty()
select new {PersonName = p.Name, Citys = t2 == null ? "" : t2.CityName};
Console.Write("------left join linq-------");
Console.WriteLine();
foreach (var item in result)
{
Console.Write(item.PersonName);
Console.Write(":");
Console.Write(item.Citys);
Console.WriteLine();
} Console.WriteLine(); //例子4(和例子2是同样的效果):左连 lamda 返回的Citys是list
var result2 = persons.GroupJoin(cities, p => p.CityID, c => c.ID,
(p, cs) => new {PersonName = p.Name, Citys = cs});
Console.Write("------left join lamda GroupJoin list-------");
Console.WriteLine();
foreach (var item in result2)
{
Console.Write(item.PersonName);
Console.Write(":");
foreach (var city in item.Citys)
{
Console.Write(city.CityName);
}
Console.WriteLine();
} Console.WriteLine(); //例子5(和例子3是同样的效果):左连 lamda
var result3 = persons.GroupJoin(cities, p => p.CityID, c => c.ID, (p, cs) => new {p, cs})
.SelectMany(p2 => p2.cs.DefaultIfEmpty(),
(p2, s2) => new {PersonName = p2.p.Name, Citys = s2 == null ? "" : s2.CityName});
Console.Write("------left join lamda GroupJoin DefaultIfEmpty--2-----");
Console.WriteLine();
foreach (var item in result3)
{
Console.Write(item.PersonName);
Console.Write(":");
Console.Write(item.Citys);
Console.WriteLine();
} Console.WriteLine();
Console.ReadKey();
}
}
}
[C#] LINQ之SelectMany和GroupJoin的更多相关文章
- [C#] LINQ之Join与GroupJoin
声明:本文为www.cnc6.cn原创,转载时请注明出处,谢谢! 一.编写Person与City类,如下: class Person { public int CityID { set; get; } ...
- [C#] LINQ之SelectMany
声明:本文为www.cnc6.cn原创,转载时请注明出处,谢谢! 一.第一种用法: public static IEnumerable<TResult> SelectMany<TSo ...
- LINQ 之 SelectMany
声明:本文为www.cnc6.cn原创,转载时请注明出处,谢谢! 一.第一种用法: public static IEnumerable<TResult> SelectMany<TSo ...
- LINQ之 Join 与 GroupJoin
声明:本文为www.cnc6.cn原创,转载时请注明出处,谢谢! 一.编写Person与City类,如下: class Person { public int CityID { set; get; } ...
- Linq入门
一.Linq需要的C#语法支持: 1.隐式变量的使用var var使用时必须初始化 var是强类型数据 2.自动属性:public string FirstName{get ;set;} 3 ...
- LINQ学习笔记 Join 与 Group join
LINQ中的Join对应T-SQL中的内连接,并无左连接的方法,当然也没有右连接. 要达成Left join必须依靠GroupJoin来完成. GroupJoin顾名思义就是先集团在做加入,加入的不同 ...
- 记一次使用 SelectMany 的经历
最近在改造一个功能时为了减少循环的层数,于是想着将List列表映射为一个能直接使用颗粒大小的List列表,这样一层循环就可以解决问题. public class ConflictWordIte ...
- Entity Framework 与 面向对象
说要分享,我了个*,写了一半放草稿箱了两个星期都快发霉了,趁着周末写完发出来吧. 文章分为五部分: 基础.类讲述的是用到的一些EF与面向对象的基础: 业务是讲怎么划分设计业务: 设计模式和工作模式讲述 ...
- 并发编程概述--C#并发编程经典实例
优秀软件的一个关键特征就是具有并发性.过去的几十年,我们可以进行并发编程,但是难度很大.以前,并发性软件的编写.调试和维护都很难,这导致很多开发人员为图省事放弃了并发编程.新版.NET 中的程序库和语 ...
随机推荐
- opencv 检测人脸、人眼
This tutorial code’s is shown lines below. You can also download it from here . The second version ( ...
- Android 使用Intent
使用intent可以吊起其他应用 例如发送电子邮件 public void sendEmail(View view){ Intent intent = new Intent(Intent.ACTION ...
- PCA与LDA
- 【转载】 AutoML相关论文
原文地址: https://www.cnblogs.com/marsggbo/p/9308518.html ---------------------------------------------- ...
- IFC数据模型构件控制
控制ifc构件的隐藏与显示.着色 osg::ref_ptr<osg::Geode> geode1 = new osg::Geode(); osg::ref_ptr<osg::Stat ...
- React——嵌入已有项目 && jsx
Add React to a Website React has been designed from the start for gradual adoption, and you can use ...
- python基础之坑爹正则表达式
python基础之坑爹正则表达式 概述 re模块就是python语言中的正则表达式,拆出来单独写一条blog是因为正则表达式本身就是比较庞大的知识,写具体些让自己以后方便查找. IP: ^(25[0- ...
- pytorch0.4.1安装
pytorch官网:https://pytorch.org/ 这里安装pytorch0.4.1版本(最新版本为1.3.0系列,但是在跑github上的一些项目时会不断地报“ UseWarinig:Le ...
- 如何让在panel里的子窗体随panel的大小改变而变化?(转)
private void Form1_Load(object sender, EventArgs e) { frm=new Form2(); ...
- LeetCode_38. Count and Say
38. Count and Say Easy The count-and-say sequence is the sequence of integers with the first five te ...