C# linq左连接与分组
1.左连接使用DefaultIfEmpty();
2.分组时候判断newper.FirstOrDefault() == null ? null: newper.ToList()这个经常出错误,如果不判断,会出现空引用的错误
class Program
{
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
} class Pet
{
public string Name { get; set; }
public Person Owner { get; set; }
}
static void Main(string[] args)
{
Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund" };
Person terry = new Person { FirstName = "Terry", LastName = "Adams" };
Person charlotte = new Person { FirstName = "Charlotte", LastName = "Weiss" };
Person arlene = new Person { FirstName = "Arlene", LastName = "Huff" }; Pet barley = new Pet { Name = "Barley", Owner = terry };
Pet boots = new Pet { Name = "Boots", Owner = terry };
Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
Pet bluemoon = new Pet { Name = "Blue Moon", Owner = terry };
Pet daisy = new Pet { Name = "Daisy", Owner = magnus }; // Create two lists.
List<Person> people = new List<Person> { magnus, terry, charlotte, arlene };
List<Pet> pets = new List<Pet> { barley, boots, whiskers, bluemoon, daisy }; var query = from person in people
join pet in pets on person equals pet.Owner into gj
from subpet in gj.DefaultIfEmpty()
group subpet by person.FirstName into newper
select new { newper.Key, Persons = newper.FirstOrDefault() == null ? null: newper.ToList() }; foreach (var ownerAndPet in query)
{
if (ownerAndPet.Persons != null)
{
foreach (var p in ownerAndPet.Persons)
{
Console.WriteLine(string.Format("{0} is owned by {1}", ownerAndPet.Key, p.Name));
} }
else
{
Console.WriteLine(string.Format("{0} is owned by {1}", ownerAndPet.Key, "没有值"));
}
} // Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
//output
//Magnus is owned by Daisy
//Terry is owned by Barley
//Terry is owned by Boots
//Terry is owned by Blue Moon
//Charlotte is owned by Whiskers
//Arlene is owned by 没有值
C# linq左连接与分组的更多相关文章
- GroupBy分组的运用和linq左连接
最简单的分组 var conHistoryList = conHistoryData.GroupBy(g => g.personId); 就是conHistoryData是一个IQueryabl ...
- Linq Left Join;linq左连接 (转载)
来源 https://www.cnblogs.com/xinjian/archive/2010/11/17/1879959.html 准备一些测试数据,如下: use Test Create tabl ...
- linq 左连接后实现与主表一对一关系数据
var query1 = from r in _residentRepository.GetAll() join i in _inLogRepository.GetAll() on r.Id equa ...
- EF to linq 左连接
如果连接的数据不存在用 null 表示,则可以左连接查询,但是如果数据类型为 int 则会出错. var ng = (from g in _db.NET_NEWS_GROUP join z in _d ...
- Linq 左连接 left join
Suppose you have a tblRoom and tblUserInfo. Now, you need to select all the rooms regardless of whet ...
- linq左连接
Table1和Table2连接,把Table1的全列出来 var tempData = from a in table1 join b in table2 on a.Id equals b.aId i ...
- linq 左连接
var list = (from item in vall join item3 in v1 on new { item.FItemID, item.FAuxPropID } equals new { ...
- linq左连接查询加上into后怎么查询右表是否为空
//判断右表是否为空并为映射表进行赋值标志var query=from q in product join m in favProduct on q.Name equals m.Name into t ...
- linq 左连接实现两个集合的合并
//第一个集合为所有的数据 var specilist = new List<Me.SpecificationsInfo>(); var resultall = (from a in db ...
随机推荐
- vue实例讲解之vue-router的使用
实例讲解系列之vue-router的使用 先总结一下vue-router使用的基本框架: 1.安装并且引入vue-router 安装:npm install vue-router --save-dev ...
- ElasticSearch入门(3) —— head插件
#### 安装ES head插件 具体请参考github地址:https://github.com/mobz/elasticsearch-head 使用 安装Install # 在线安装head插件 ...
- Spring-Boot:Spring Cloud构建微服务架构
概述: 从上一篇博客<Spring-boot:5分钟整合Dubbo构建分布式服务> 过度到Spring Cloud,我们将开始学习如何使用Spring Cloud 来搭建微服务.继续采用上 ...
- Cow Uncle 学习了叉积的一点运用,叉积真的不错
Cow Uncle Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitSta ...
- CentOS 搭建Graylog集群详解
1. Graylog2 简介 Graylog 是一个简单易用.功能较全面的日志管理工具,相比 ELK 组合, 优点: 部署维护简单,一体化解决方案,不像ELK三个独立系统集成. 查相比ES json语 ...
- 简单Elixir游戏服务器-安装Elixir
用WebInstaller 安装半天也没下载成功文件. 改成直接下载erlang 和 elixir 预编译包了. 安装很简单,最后设置好环境变量. cmd 执行 elixir -v 最后顺便下载了个g ...
- JavaWeb之response响应中文乱码问题
response向页面响应中文乱码问题 字节流 * 有可能乱码,与中文转换成字节数组.浏览器打开的默认字符编码有关 * 解决方式:将中文转成字节数组的时候和浏览器默认打开的时候采用的字符集一致 re ...
- python - 常用模块栗子
前言 内容摘自Python参考手册(第4版) 及 自己测试后得出.仅作为一个简单记录,方便使用查询之用. 另 dir(func)及func.__doc__可以查看对象属性及说明文档. 序列Seque ...
- 【转载】quickLayout.css-快速构建结构兼容的web页面
文章转载自 张鑫旭-鑫空间-鑫生活 http://www.zhangxinxu.com/wordpress/ 原文链接:http://www.zhangxinxu.com/wordpress/?p=4 ...
- nginx+tomcat+redis sesson id主从复制
Redis与Memcached的区别: 内存利用率:使用简单的key-value存储的话,Memcached的内存利用率更高,而如果Redis采用hash结构来做key-value存储, ...