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左连接与分组的更多相关文章

  1. GroupBy分组的运用和linq左连接

    最简单的分组 var conHistoryList = conHistoryData.GroupBy(g => g.personId); 就是conHistoryData是一个IQueryabl ...

  2. Linq Left Join;linq左连接 (转载)

    来源 https://www.cnblogs.com/xinjian/archive/2010/11/17/1879959.html 准备一些测试数据,如下: use Test Create tabl ...

  3. linq 左连接后实现与主表一对一关系数据

    var query1 = from r in _residentRepository.GetAll() join i in _inLogRepository.GetAll() on r.Id equa ...

  4. EF to linq 左连接

    如果连接的数据不存在用 null 表示,则可以左连接查询,但是如果数据类型为 int 则会出错. var ng = (from g in _db.NET_NEWS_GROUP join z in _d ...

  5. Linq 左连接 left join

    Suppose you have a tblRoom and tblUserInfo. Now, you need to select all the rooms regardless of whet ...

  6. linq左连接

    Table1和Table2连接,把Table1的全列出来 var tempData = from a in table1 join b in table2 on a.Id equals b.aId i ...

  7. linq 左连接

    var list = (from item in vall join item3 in v1 on new { item.FItemID, item.FAuxPropID } equals new { ...

  8. linq左连接查询加上into后怎么查询右表是否为空

    //判断右表是否为空并为映射表进行赋值标志var query=from q in product join m in favProduct on q.Name equals m.Name into t ...

  9. linq 左连接实现两个集合的合并

    //第一个集合为所有的数据 var specilist = new List<Me.SpecificationsInfo>(); var resultall = (from a in db ...

随机推荐

  1. 记2017问鼎杯预赛的wp---来自一个小菜鸡的感想

    这次准备写一下几个misc和密码题目..很坑. 打了一整天的比赛,越来越觉得自己很菜了. 有一道题目叫做"真真假假",这道题目只有一个提示--Xor.第一眼知道是异或,也就知道这一 ...

  2. HDU 5974 数学

    A Simple Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  3. struts2中的Ajax异步校验

    登录时验证码的异步校验: 1.验证码生成的是图片因此在struts.xml文件里面配置action 时,result标签中type 属性是stream 2.验证码图片的src的值为配置action名字 ...

  4. 一脸懵逼学习基于CentOs的Hadoop集群安装与配置

    1:Hadoop分布式计算平台是由Apache软件基金会开发的一个开源分布式计算平台.以Hadoop分布式文件系统(HDFS)和MapReduce(Google MapReduce的开源实现)为核心的 ...

  5. FPGA与安防领域

    安防主要包括:闭路监控系统.防盗报警系统.楼宇对讲系统.停车厂管理系统.小区一卡通系统.红外周界报警系统.电子围栏.巡更系统.考勤门禁系统.安防机房系统.电子考场系统.智能门锁等等. 在监控系统中,F ...

  6. HDU1403Longest Common Substring

    明天写 超时代码: #include<cstdio> #include<cstdlib> #include<iostream> #include<cstrin ...

  7. C# 多线程、异步线程、线程池相关知识

    /* 线程池ThreadPool类会在需要时增减池中线程的线程数,直到最大的线程数.池中的最大线程数是可配置的. 在双核CPU中,默认设置为1023个工作线程和1000个I/O线程.也可以指定在创建线 ...

  8. 网站常用的一些javascript封装 简化调用

    //用于网页地址参数 //参数中包含出了英文中文数字之外的其他符号时进行编码并在前面加"=="进行标识,否则直接返回 //解码时根据是否含有"=="标识来决定是 ...

  9. WPF:获取DataGrid控件单元格DataGridCell

    转载:http://blog.csdn.net/jhqin/article/details/7645357 /* ------------------------------------------- ...

  10. iOS10新特性之SiriKit

    在6月14日凌晨的WWDC2016大会上,苹果提出iOS10是一次里程碑并且推出了十个新特性,大部分的特性是基于iPhone自身的原生应用的更新,具体的特性笔者不在这里再次叙述,请看客们移步WWDC2 ...