代码:

    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());
}
}

对list操作

上面的代码可以不看,反正就是一些临时用的数据

ActionPerson ap = new ActionPerson();
List<Person> list = ap.GetPersons();
ap.Show(list);
Console.WriteLine("========================================");
List<Dog> dogs = ap.GetDogs();
ap.Show(dogs);
Console.WriteLine("========================================");

上面的是list,下面的是dogs

ap(dogClothes);

 1.取有狗的人

 var list2 = from a in list from b in dogs where a.ID == b.PersonID select a;
ap.Show(list2.ToList());
Console.WriteLine("========================================");
ap.Show(list2.Distinct().ToList());
Console.WriteLine("========================================");

发现,自连接就是左表一条条数据与右表一条条数据交叉,取得符合条件的(a.ID==b.PersonID),再取得需要的字段(a),不会自动合并。

看这个就清楚了

var list4 = from a in list from b in dogs where a.ID == b.PersonID select new { a.ID, a.Name, a.Age, DogsID = b.ID, DogsName = b.Name };
ap.Show(list4.ToList());

这样上面的就不要区分(distinct)了,之后再取需要的字段

2.join

  var list2 = from a in list join b in dogs on a.ID equals b.PersonID into c select new { a.ID, a.Name, a.Age,Count=c.Count() };
ap.Show(list2.ToList());

在上面这种into一个对象时,会发现b不能够调用了,a还是可以使用的,当然新形成的对象c也可以,c有一些特殊的属性,例如count

            var list2 = from a in list join b in dogs on a.ID equals b.PersonID select new { a.ID, a.Name, a.Age, DogsID = b.ID, DogsName = b.Name, b.PersonID };

 var list2 = from a in list join b in dogs on a.ID equals b.PersonID select new { a.ID, a.Name, a.Age };
ap.Show(list2.ToList());

跟自连接一样

LINQ TO SQL中的join,如果带有into,可以提前对表进行过滤条件的操作,而不用等到两表进行迪卡尔积产生虚似表后再进行join on的条件过滤。

关于left join

 var list2 = from a in list
join b in dogs on a.ID equals b.PersonID into c
from o in c.DefaultIfEmpty()
select new
{
a.ID,
a.Name,
a.Age,
DogsID =o==null?: o.ID,
DogsName = o == null ? "" : o.Name,
PersonID=o==null?:o.PersonID
};

在数据库中就不需要这么复杂,这里list中要考虑右侧可能为空,

数据库查询就不需要。

多查询

  static void Main(string[] args)
{
ActionPerson ap = new ActionPerson();
List<Person> list = ap.GetPersons();
List<Dog> dogs = ap.GetDogs();
List<DogClothes> dogClotheses = ap.GetDogClotheses();
var result = from a in list
join b in dogs
on a.ID equals b.PersonID
join c in dogClotheses
on b.ID equals c.DogID
select new
{
a.ID ,
a.Name ,
a.Age,
DogID=b.ID ,
DogName=b.Name,
b.PersonID ,
DogClothesID=c.ID,
DogClothesName=c.Name ,
DogID2=c.DogID
};
ap.Show(result.ToList ());
Console.ReadLine();
}

   var result = from a in list
from b in dogs
from c in dogClotheses
where a.ID == b.PersonID && b.ID == c.DogID
select new
{
a.ID,
a.Name,
a.Age,
DogID = b.ID,
DogName = b.Name,
b.PersonID,
DogClothesID = c.ID,
DogClothesName = c.Name,
DogID2 = c.DogID
};

结果上同

http://developer.51cto.com/art/200909/152189.htm

http://www.cnblogs.com/ASPNET2008/archive/2008/12/21/1358152.html

Linq 对List的一些操作的更多相关文章

  1. C# LINQ系列:LINQ to DataSet的DataTable操作 及 DataTable与Linq相互转换

    LINQ to DataSet需要使用System.Core.dll.System.Data.dll和System.Data.DataSetExtensions.dll,在项目中添加引用System. ...

  2. Linq对XML的简单操作

    前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...

  3. LINQ to SQL更新数据库操作(转载)

    使用LINQ to SQL建模Northwind数据库 在这之前一起学过LINQ to SQL设计器的使用,下面就使用如下的数据模型: 当使用LINQ to SQL设计器设计以上定义的五个类(Prod ...

  4. LINQ to SQL的CRUD操作

    创建数据对象模型 sqlmetal /code:"C:\MyProjects\VS2008\Data\LinqConsoleApp2\LinqConsoleApp2\northwnd.cs& ...

  5. 【开发者笔记】Linq 多表关联排序操作

    c# 一直是一门好用的语言,但是像linq这种骚操作实在是记不住.特此记下以备后用. var ls = from c in db.T_ProductReturnEntity join s in db. ...

  6. LINQ系列:LINQ to DataSet的DataTable操作

    LINQ to DataSet需要使用System.Core.dll.System.Data.dll和System.Data.DataSetExtensions.dll,在项目中添加引用System. ...

  7. LINQ系列:LINQ to DataSet的DataView操作

    1. 创建DataView EnumerableRowCollection<DataRow> expr = from p in products.AsEnumerable() orderb ...

  8. MVC linq To SQL更新数据库操作

    首先在视图中提交数据,使用Html.BeginForm() @using(Html.BeginForm()) { @Html.EditorForModel() //编辑模板.控制器中传过来的数据 &l ...

  9. LINQ to DataSet的DataTable操作

    1. DataTable读取列表 DataSet ds = new DataSet();// 省略ds的Fill代码DataTable products = ds.Tables["Produ ...

随机推荐

  1. LoadRunner-关联问题(栏目列表较多关联不了想要的id)

    新建了课程后之后有很多栏目,每个栏目对应一个partid,但我只想要期中一个. http://*********/course/work/workInfo.action?hwid=1547&c ...

  2. js判断字符串长度

    方法1: String.prototype.gblen = function() { var len = 0; for (var i=0; i<this.length; i++) { if (t ...

  3. Kubernetes网络的4种解决方案

    一.Kubernetes + Flannel Kubernetes的网络模型假定了所有Pod都在一个可以直接连通的扁平的网络空间中,这在GCE(Google Compute Engine)里面是现成的 ...

  4. 对Django框架架构和Request/Response处理流程的分析(转)

    原文:http://blog.sina.com.cn/s/blog_8a18c33d010182ts.html 一. 处理过程的核心概念 如下图所示django的总览图,整体上把握以下django的组 ...

  5. 查看修改MySQL字符集

    查看修改MySQL字符集 http://blog.sina.com.cn/s/blog_70ac6bec01016fts.html 查看修改MySQL字符集 (2012-08-22 09:53:21) ...

  6. [py]列表生成式-支持条件,多值的拼接

    列表生成式 代码简洁一些 支持多条件, 过滤,或拼接某些值 支持返回多值 是一种生成式 # 生成一个列表 print(list(range(1, 11))) # 生成一个列表x^2 ## 方法1: 返 ...

  7. Ubuntu16.04 安装 “宋体,微软雅黑,Consolas雅黑混合版编程字体” 等 Windows 7 下的字体

    Windows平台下,“宋体”.“微软雅黑”.“Courier New(编程字体)”用的比较多,看的也习惯了.那如何在 Ubuntu下也安装这些字体呢? 操作步骤如下: 第一步:从 Windows 7 ...

  8. Locust性能测试3-no-web模式和csv报告保存

    前言 前面是在web页面操作,需要手动的点start启动,结束的时候也需要手工去点stop,没法自定义运行时间,这就不太方便. locust提供了命令行运行的方法,不启动web页面也能运行,这就是no ...

  9. 接口自动化测试框架搭建 – Java+TestNG 测试Restful service

    接口自动化测试 – Java+TestNG 测试 Restful Web Service 关键词:基于Rest的Web服务,接口自动化测试,数据驱动测试,测试Restful Web Service, ...

  10. CUDA显卡运算编程菜鸟入门指南1——Hello world - yfszzx的专栏 - 博客频道 - CSDN.NET

    第一次知道有显卡(GPU)编程这个东西,是去年比特币最热门的时候,看了几篇关于比特币的文章,说比特币挖矿要靠显卡,CPU的速度与GPU根本就没法比,于是就非常好奇,显卡是什么神奇的东西?为什么运算速度 ...