Linq in GroupBy GroupJoin
还是上一份的代码例子:
public class Person
{
public int ID { get; set; } public string Name { get; set; } public int Age { get; set; }
}
public class Dog
{
public int ID { get; set; }
public string Name { get; set; }
public int PersonID { get; set; }
}
public class DogClothes
{
public int ID { get; set; }
public string Name { get; set; }
public int DogID { get; set; }
}
public class ActionPerson
{
public List<Person> GetPersons()
{
List<Person> list = new List<Person>();
for (var i = ; i < ; i++)
{
Person p = new Person {ID=i+, Name = "hongda" + (i + ), Age = + i };
list.Add(p);
}
return list;
}
public List<Dog> GetDogs()
{
List<Dog> dogs = new List<Dog>();
for (var i = ; i < ; i++)
{
Dog dog = new Dog { ID = i, Name = "dogs" + i, PersonID = (i %)+ };
dogs.Add(dog);
}
return dogs;
}
public List<DogClothes> GetDogClotheses()
{
List<DogClothes> DogClothes = new List<DogClothes>();
for (var i = ; i < ; i++)
{
DogClothes clothes = new DogClothes() { ID = i, Name = "DogClothes" + i, DogID = (i % ) + };
DogClothes.Add(clothes);
}
return DogClothes;
}
public void Show<T>(List<T> list)
{
foreach (var l in list)
{
ShowObj(l);
}
}
public void ShowObj<T>(T data)
{
Type t = data.GetType();
PropertyInfo[] infos = t.GetProperties();
StringBuilder strBuilder = new StringBuilder();
foreach (var p in infos)
{
object value = p.GetValue(data, null);
string name = p.Name.ToString();
strBuilder.Append(name + ":" + value + ",");
}
Console.WriteLine(strBuilder.ToString());
}
public void ShowArray<T>(T[] arr)
{
foreach (var t in arr)
{
Console.WriteLine(t.ToString());
}
}
}
操作:
static void Main(string[] args)
{
ActionPerson ap = new ActionPerson();
List<Person> list = ap.GetPersons();
List<Dog> dogs = ap.GetDogs();
List<DogClothes> dogClotheses = ap.GetDogClotheses();
int[] arr = { , , , };
var result = list.Where(q => arr.Contains(q.ID));
ap.Show(result.ToList ());
Console.WriteLine("=============================");
var result2 = list.Where(q => dogs.Select(o => o.PersonID).Contains(q.ID));
ap.Show(result2.ToList());
Console.ReadLine();
}

var result = list.Select(o => o.Name);
ap.Show(result.ToList());

再看
var result = list.Select(o => o.Name);
ap.ShowArray(result.ToArray());

可以看出Select(o=>o.Name) 返回的是数组
返回对象
var result = list.Select(p => new { ID = p.ID, p.Name, p.Age });
ap.Show(result.ToList ());

对象数组
var result = list.Select(p => new { ID = p.ID, p.Name, p.Age });
ap.ShowArray(result.ToArray());

Group Join
var result = from a in list
join b in dogs on a.ID equals b.PersonID into c
select new { a, c };
ap.Show(result.ToList());

var result = from a in list
join b in dogs on a.ID equals b.PersonID into c
select new { a, c };
foreach (var r in result)
{
ap.ShowObj(r.a);
ap.Show(r.c.ToList());
Console.WriteLine("========================================");
}

var result = list.GroupJoin(dogs, a => a.ID, b => b.PersonID, (a, c) => new {a,c });
结果同上
c还有一些其他的属性
GroupBy
var result = from b in dogs group b by b.PersonID into g select new { g.Key, Count = g.Count(), Member = g };
foreach (var r in result)
{
Console.WriteLine(r.Key + "," + r.Count + ",");
foreach (var m in r.Member)
{
ap.ShowObj(m);
}
Console.WriteLine("=================================");
}

var result = (from b in dogs select b).GroupBy(q => q.PersonID).Select(q => new { q.Key, Count = q.Count(), Member = q });
结果同上
Linq in GroupBy GroupJoin的更多相关文章
- Linq分组操作之GroupBy,GroupJoin扩展方法源码分析
Linq分组操作之GroupBy,GroupJoin扩展方法源码分析 一. GroupBy 解释: 根据指定的键选择器函数对序列中的元素进行分组,并且从每个组及其键中创建结果值. 查询表达式: var ...
- Linq Mysql GroupBy语句的问题处理
语句如下: var resumeList = db.ChannelResume.Where(model); var groupValues = resumeList.GroupBy(t => n ...
- linq lambda GroupBy 用法
Linq 中按照多个值进行分组(GroupBy) /// <summary>要查询的对象</summary> class Employee { public int ID ...
- 转载有个小孩跟我说LINQ(重点讲述Linq中GroupBy的原理及用法)
转载原出处: http://www.cnblogs.com/AaronYang/archive/2013/04/02/2994635.html 小孩LINQ系列导航:(一)(二)(三)(四)(五)(六 ...
- 转载Linq中GroupBy方法的使用总结
Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均值等. Linq中的Groupby方法也有这种功能.具体实现看代码: 假设有如下的一个数据集: public class St ...
- LINQ 之 GroupBy
声明:本文为www.cnc6.cn原创,转载时请注明出处,谢谢! 本文作者文采欠佳,文字表达等方面不是很好,但实际的代码例子是非常实用的,请作参考. 一.先准备要使用的类: 1.Person类: cl ...
- [C#] LINQ之GroupBy
声明:本文为www.cnc6.cn原创,转载时请注明出处,谢谢! 本文作者文采欠佳,文字表达等方面不是很好,但实际的代码例子是非常实用的,请作参考. 一.先准备要使用的类: 1.Person类: cl ...
- Linq中GroupBy方法的使用总结(转)
Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均值等. Linq中的Groupby方法也有这种功能.具体实现看代码: 假设有如下的一个数据集: public class St ...
- Linq中GroupBy方法的使用总结(转载)
from:https://www.cnblogs.com/zhouzangood/articles/4565466.html Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均 ...
随机推荐
- matplotlib基本使用方法
[微语]人生有可为之事,也有不可为之事.可为之事,当尽力为之,此谓尽性,不可为之事,当尽心为之,此谓知命. 三人行必有我师 官方参考API:https://matplotlib.org/tutoria ...
- 005-四种常见的 POST 提交数据方式
1.http请求方法 HTTP Method RFC Request Has Body Response Has Body Safe Idempotent Cacheable GET RFC 7231 ...
- word使用
1:插入图片,显示不完整,需要>点击上方的段落,选择单倍行距 2:wps 可以直接右键选择保存文件中的图片 3:word中换行符的标识符为^p ,可以用来替换换行符. 4:使word中某一段背 ...
- Selenium之Css Selector使用方法
什么是Css Selector? Css Selector定位实际就是HTML的Css选择器的标签定位 工具 Css Selector的练习建议使用火狐浏览器,下载插件,FireFinder.Fire ...
- [py]可迭代对象-求最值
for .. in ..方式遍历可迭代对象 而非下标 ## 判断是否可迭代 from collections import Iterable print(isinstance(123,Iterable ...
- [LeetCode] 102. Binary Tree Level Order Traversal_Medium tag: BFS
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 机器学习理论基础学习3.2--- Linear classification 线性分类之线性判别分析(LDA)
在学习LDA之前,有必要将其自然语言处理领域的LDA区别开来,在自然语言处理领域, LDA是隐含狄利克雷分布(Latent Dirichlet Allocation,简称LDA),是一种处理文档的主题 ...
- TCP状态转换图、滑动窗口、半连接状态、2MSL
一.TCP状态转换图 下图对排除和定位网络或系统故障时大有帮助,也帮助我们更好的编写Linux程序,对嵌入式开发也有指导意义. 先回顾一下TCP建立连接的三次握手过程,以及关闭连接的四次握手过程 ...
- 不用中间变量交换a 和b的值
// 不用中间变量的写法 ,假如 a=13, b=8; a=a+b =21; //此时 a=21; b=8; b=a-b=13; //此时a=21; b=13; a=a-b=8; //相当于 a=21 ...
- Functional Programming 资料收集
书籍: Functional Programming for Java Developers SICP(Structure and Interpretation of Computer Program ...