还是上一份的代码例子:

  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的更多相关文章

  1. Linq分组操作之GroupBy,GroupJoin扩展方法源码分析

    Linq分组操作之GroupBy,GroupJoin扩展方法源码分析 一. GroupBy 解释: 根据指定的键选择器函数对序列中的元素进行分组,并且从每个组及其键中创建结果值. 查询表达式: var ...

  2. Linq Mysql GroupBy语句的问题处理

    语句如下: var resumeList = db.ChannelResume.Where(model); var groupValues = resumeList.GroupBy(t => n ...

  3. linq lambda GroupBy 用法

    Linq 中按照多个值进行分组(GroupBy)   /// <summary>要查询的对象</summary> class Employee { public int ID ...

  4. 转载有个小孩跟我说LINQ(重点讲述Linq中GroupBy的原理及用法)

    转载原出处: http://www.cnblogs.com/AaronYang/archive/2013/04/02/2994635.html 小孩LINQ系列导航:(一)(二)(三)(四)(五)(六 ...

  5. 转载Linq中GroupBy方法的使用总结

    Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均值等. Linq中的Groupby方法也有这种功能.具体实现看代码: 假设有如下的一个数据集: public class St ...

  6. LINQ 之 GroupBy

    声明:本文为www.cnc6.cn原创,转载时请注明出处,谢谢! 本文作者文采欠佳,文字表达等方面不是很好,但实际的代码例子是非常实用的,请作参考. 一.先准备要使用的类: 1.Person类: cl ...

  7. [C#] LINQ之GroupBy

    声明:本文为www.cnc6.cn原创,转载时请注明出处,谢谢! 本文作者文采欠佳,文字表达等方面不是很好,但实际的代码例子是非常实用的,请作参考. 一.先准备要使用的类: 1.Person类: cl ...

  8. Linq中GroupBy方法的使用总结(转)

    Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均值等. Linq中的Groupby方法也有这种功能.具体实现看代码: 假设有如下的一个数据集: public class St ...

  9. Linq中GroupBy方法的使用总结(转载)

    from:https://www.cnblogs.com/zhouzangood/articles/4565466.html Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均 ...

随机推荐

  1. LoadRunner-迭代和并发设置

    迭代:指运行一次脚本时某段代码块(action)循环执行的次数,串行执行 并发:指同时运行脚本的次数,并行执行(多个用户同时跑) 以下是用例和对应的相关设置 Iterations是在Vuser Gen ...

  2. 构建更好的客户端 JavaScript 应用

    你可能注意到了,最近的一段时间越来越多的Web应用有变复杂的趋势,重心从服务端慢慢向着客户端转移. 这是个正常的趋势么?我不知道.支持和反对者的讨论就像是在讨论复活者和圣诞节哪一个更好一样; 很难说哪 ...

  3. mysql python pymysql模块 增删改查 查询 fetchone

    import pymysql mysql_host = '192.168.0.106' port = 3306 mysql_user = 'root' mysql_pwd = ' encoding = ...

  4. UIAlertview 添加图片

    - (void)willPresentAlertView:(UIAlertView *)alertView { 在这个方法中, 绘制需要的东西 uiview *myView = [uiview all ...

  5. 查看Django和flask版本

    查看Django版本 检查是否安装成功,可以在dos下查看Django版本. 1.输入python 2.输入import django 3.输入django.get_version() 1 2 3 4 ...

  6. categoriy 重写函数会怎样?

    From comp.lang.objective-C FAQ listing: "What if multiple categories implement the same method? ...

  7. pandas取dataframe特定行/列

    1. 按列取.按索引/行取.按特定行列取 import numpy as np from pandas import DataFrame import pandas as pd df=DataFram ...

  8. [lr] 基本色调调整和色调曲线

    基本色调调整 • 曝光度调整 ▶ 控制区域 在Lightroom中,软件提示我们曝光控制的是如图中间调的区域.我们把鼠标移动到曝光工具条上,软件会提示我们这个区域: ▶ 实际效果 ▪ 增加曝光值 增加 ...

  9. c++四种强制类型转化

    c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast C++ 类型转换(C风格的强制转换): (1)将浮点型数据赋值给整型变 ...

  10. Input消除自动记忆功能

    在html里就可以直接清除了<input type="text" autocomplete="off"> input 的autocomplete属性 ...