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 ...
随机推荐
- MySQL高级查询(一)
修改表 修改表名 语法: ALTER TABLE<旧表名> RENAME [TO] <新表名>; 添加字段 语法: ALTER TABLE 表名 ADD 字段名 数据类型 ...
- java中堆栈的功能作用 以及區別(搜集)
1.用new创建的对象在堆区,函数中的临时变量在栈区,Java中的字符串在字符串常量区. 2.栈:存放进本数据类型的数据和对象的引用,但对象本身不存在栈中,而是存放在堆中. 堆:存放new产生 ...
- Integer的自动拆箱
public class Test2{ public static void main(String[] args){ Integer a=1; Integer b=2; Integer c=3; I ...
- SUM游戏
题意:就是有一个长度为n的整数序列,两个游戏者A和B轮流取数,A先取.每次玩家只能从左端或者右端取任意数量个数,但不能两端都取. 所有数都被取走后游戏结束,然后统计每个人取走的所有数之和,作为各自的得 ...
- Day2 python基础学习
http://www.pythondoc.com/ Python中文学习大本营 本节内容: 一.字符串操作 二.列表操作 三.元组操作 四.字典操作 五.集合操作 六.字符编码操作 一.字符串操作 1 ...
- WPF:获取DataGrid控件单元格DataGridCell
转载:http://blog.csdn.net/jhqin/article/details/7645357 /* ------------------------------------------- ...
- Oracle RAC + ASM + Grid安装
(一)环境准备 主机操作系统 windows10 虚拟机平台 vmware workstation 12 虚拟机操作系统 redhat 5.5 x86(32位) :Linux.5.5.for.x86. ...
- 运用 finereport 和 oracle 结合开发报表思路大总结
近排自己学习了一款软件finereport开发报表模块,自己总结了如何了解需求,分析需求,再进行实践应用开发,最后进行测试数据的准确性,部署报表到项目对应的模块中显示. 一.需求(根据需求文档分析) ...
- OpenGL ES2.0光照
一.简单光照原理 平行光(正常光) 光照效果= 环境颜色 + 漫反射颜色 + 镜面反射颜色 点光源 光照效果= 环境颜色 + (漫反射颜色 + 镜面反射颜色)× 衰减因子 聚光灯 光照效果= ...
- C语言第一次实验报告————PTA实验1.2.3内容
一.PTA实验作业 题目1.温度转换 本题要求编写程序,计算华氏温度100°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代 ...