复杂的查询对象,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinqDemo
{
class Customer
{
public string ID { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public decimal Sales { get; set; }
public override string ToString()
{
return "ID:" + ID + " City:" + City + " Country:" + Country + " Region:" + Region + " Sales:" + Sales;
}
}
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer>
{
new Customer {ID="A",City="New York",Country="USA",Region="North America",Sales=999 },
new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
}; var queryResults = from c in customers
where c.Region == "Asia"
select c;
Console.WriteLine("亚洲的有:");
foreach (Customer c in queryResults)
{
Console.WriteLine(c);
} Console.ReadLine();
}
}
}

查询对象的某个元素

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinqDemo
{
class Customer
{
public string ID { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public decimal Sales { get; set; }
public override string ToString()
{
return "ID:" + ID + " City:" + City + " Country:" + Country + " Region:" + Region + " Sales:" + Sales;
}
}
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer>
{
new Customer {ID="A",City="New York",Country="USA",Region="North America",Sales=999 },
new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
}; var queryResults = from c in customers
where c.Region == "Asia"
select c.City;
Console.WriteLine("亚洲的城市有:");
foreach (String city in queryResults)
{
Console.WriteLine(city);
} Console.ReadLine();
}
}
}

查询结果只能是字符串,或者一个对象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinqDemo
{
class Customer
{
public string ID { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public decimal Sales { get; set; }
public override string ToString()
{
return "ID:" + ID + " City:" + City + " Country:" + Country + " Region:" + Region + " Sales:" + Sales;
}
}
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer>
{
new Customer {ID="A",City="New York",Country="USA",Region="North America",Sales=999 },
new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
}; var queryResults = from c in customers
where c.Region == "Asia"
select new { c.City, c.Country };
Console.WriteLine("亚洲的有:");
foreach (Object item in queryResults)
{
Console.WriteLine(item);
} Console.ReadLine();
}
}
}

用Select函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinqDemo
{
class Customer
{
public string ID { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public decimal Sales { get; set; }
public override string ToString()
{
return "ID:" + ID + " City:" + City + " Country:" + Country + " Region:" + Region + " Sales:" + Sales;
}
}
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer>
{
new Customer {ID="A",City="New York",Country="USA",Region="North America",Sales=999 },
new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
}; //var queryResults = from c in customers
// where c.Region == "Asia"
// select new { c.City, c.Country };
var queryResults = customers.Where(c => c.Region == "Asia").Select(c => new { c.City, c.Country });
Console.WriteLine("亚洲的有:");
foreach (Object item in queryResults)
{
Console.WriteLine(item);
} Console.ReadLine();
}
}
}

去重处理Distinct方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinqDemo
{
class Customer
{
public string ID { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public decimal Sales { get; set; }
public override string ToString()
{
return "ID:" + ID + " City:" + City + " Country:" + Country + " Region:" + Region + " Sales:" + Sales;
}
}
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer>
{
new Customer {ID="A",City="New York",Country="USA",Region="North America",Sales=999 },
new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
}; //var queryResults = from c in customers
// where c.Region == "Asia"
// select new { c.City, c.Country };
var queryResults = customers.Select(c => c.Region).Distinct();
Console.WriteLine("洲有:");
foreach (Object item in queryResults)
{
Console.WriteLine(item);
} Console.ReadLine();
}
}
}

any,All查询

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinqDemo
{
class Customer
{
public string ID { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public decimal Sales { get; set; }
public override string ToString()
{
return "ID:" + ID + " City:" + City + " Country:" + Country + " Region:" + Region + " Sales:" + Sales;
}
}
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer>
{
new Customer {ID="A",City="New York",Country="USA",Region="North America",Sales=999 },
new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
}; // Any All
bool anyUSA = customers.Any(c => c.Country == "USA");
if (anyUSA)
{
Console.WriteLine("有些人是美国的");
}
else
{
Console.WriteLine("没有美国的");
} bool allAsia = customers.All(c => c.Region == "Asia");
if (allAsia)
{
Console.WriteLine("全是亚洲的人");
}
else
{
Console.WriteLine("不全是亚洲的人");
} Console.ReadLine();
}
}
}

多级排序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinqDemo
{
class Customer
{
public string ID { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public decimal Sales { get; set; }
public override string ToString()
{
return "ID:" + ID + " City:" + City + " Country:" + Country + " Region:" + Region + " Sales:" + Sales;
}
}
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer>
{
new Customer {ID="A",City="New York",Country="USA",Region="North America",Sales=999 },
new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
}; var queryResults = from c in customers
orderby c.Region,c.Country,c.City // 多级排序
select new {c.Region , c.Country, c.City };
Console.WriteLine("亚洲的有:");
foreach (Object item in queryResults)
{
Console.WriteLine(item);
} Console.ReadLine();
}
}
}

使用OrderBy,ThenBy实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinqDemo
{
class Customer
{
public string ID { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public decimal Sales { get; set; }
public override string ToString()
{
return "ID:" + ID + " City:" + City + " Country:" + Country + " Region:" + Region + " Sales:" + Sales;
}
}
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer>
{
new Customer {ID="A",City="New York",Country="USA",Region="North America",Sales=999 },
new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
}; //var queryResults = from c in customers
// orderby c.Region,c.Country,c.City // 多级排序
// select new {c.Region , c.Country, c.City };
var queryResults = customers.OrderBy(c => c.Region)
.ThenBy(c => c.Country)
.ThenBy(c => c.City)
.Select(c => new { c.Region,c.Country, c.City}); Console.WriteLine("亚洲的有:");
foreach (Object item in queryResults)
{
Console.WriteLine(item);
} Console.ReadLine();
}
}
}

group的使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinqDemo
{
class Customer
{
public string ID { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public decimal Sales { get; set; }
public override string ToString()
{
return "ID:" + ID + " City:" + City + " Country:" + Country + " Region:" + Region + " Sales:" + Sales;
}
}
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer>
{
new Customer {ID="A",City="New York",Country="USA",Region="North America",Sales=999 },
new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
}; var queryResults = from c in customers
group c by c.Region into cg
select new { TotalSales = cg.Sum(c => c.Sales),Region = cg.Key }; var orderedResults =
from cg in queryResults
orderby cg.TotalSales descending
select cg; foreach (var item in orderedResults)
{
Console.WriteLine(item.TotalSales+"\t:"+item.Region);
} Console.ReadLine();
}
}
}

通过Take方法获取前几个数据

通过Skip方法,获取剩下的数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinqDemo
{
class Customer
{
public string ID { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public decimal Sales { get; set; }
public override string ToString()
{
return "ID:" + ID + " City:" + City + " Country:" + Country + " Region:" + Region + " Sales:" + Sales;
}
}
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer>
{
new Customer {ID="A",City="New York",Country="USA",Region="North America",Sales=999 },
new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
}; var queryResults = from c in customers
group c by c.Region into cg
select new { TotalSales = cg.Sum(c => c.Sales),Region = cg.Key }; var orderedResults =
from cg in queryResults
orderby cg.TotalSales descending
select cg; // Take获取前几个数据
// Skip获取后几个数据
// 出去前1个后的数据
foreach (var item in orderedResults.Skip(1))
{
Console.WriteLine(item.TotalSales + "\t:" + item.Region);
} Console.WriteLine("----------------------------------------"); // 前1个
foreach (var item in orderedResults.Take(1))
{
Console.WriteLine(item.TotalSales + "\t:" + item.Region);
} Console.ReadLine();
}
}
}

First与FirstOrDefault

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinqDemo
{
class Customer
{
public string ID { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public decimal Sales { get; set; }
public override string ToString()
{
return "ID:" + ID + " City:" + City + " Country:" + Country + " Region:" + Region + " Sales:" + Sales;
}
}
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer>
{
new Customer {ID="A",City="New York",Country="USA",Region="North America",Sales=999 },
new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
}; var queryResults = from c in customers
group c by c.Region into cg
select new { TotalSales = cg.Sum(c => c.Sales),Region = cg.Key }; var orderedResults =
from cg in queryResults
orderby cg.TotalSales descending
select cg; // 获取第一个数据,First和FirstOrDefault
// First方法如何数据不存在,会报错
Console.WriteLine(orderedResults.First()); Console.WriteLine(orderedResults.FirstOrDefault()); Console.WriteLine(orderedResults.First(c => c.Region == "Europe")); Console.WriteLine(orderedResults.FirstOrDefault(c => c.Region == "Europe"));
Console.ReadLine();
}
}
}

JOIN使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinqDemo
{
class Customer
{
public string ID { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public decimal Sales { get; set; }
public override string ToString()
{
return "ID:" + ID + " City:" + City + " Country:" + Country + " Region:" + Region + " Sales:" + Sales;
}
} class Order
{
public string ID { get; set; }
public decimal Amount { get; set; }
} class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer>
{
new Customer {ID="A",City="New York",Country="USA",Region="North America",Sales=999 },
new Customer {ID="B",City="Bei Jing",Country="China",Region="Asia",Sales=1999 },
new Customer {ID="C",City="Shang Hai",Country="China",Region="Asia",Sales=9999 },
new Customer {ID="D",City="Tokyo",Country="Japan",Region="Asia",Sales=2999 },
new Customer {ID="E",City="London",Country="England",Region="Europe",Sales=3999 }
}; List<Order> orders = new List<Order>
{
new LinqDemo.Order {ID = "A",Amount = 100 },
new LinqDemo.Order {ID = "A",Amount = 200 },
new LinqDemo.Order {ID = "B",Amount = 300 },
new LinqDemo.Order {ID = "B",Amount = 400 },
new LinqDemo.Order {ID = "C",Amount = 500 },
new LinqDemo.Order {ID = "D",Amount = 600 },
}; var queryResults = from c in customers
join o in orders on c.ID equals o.ID
select new { c.ID,c.City,SalesBefore = c.Sales,NewOrder = o.Amount,SalesAfter = c.Sales + o.Amount }; foreach (var item in queryResults)
{
Console.WriteLine(item);
} Console.ReadLine();
}
}
}

Linq复杂对象查询的更多相关文章

  1. 转linq中的Single()、First()、Take(1) LINQ 标准的查询操作符 排序 orderby、thenby、Take

    Single():操作一个集合,同时强要求只有一个对象匹配,并返回这一个. First():操作一个集合,可以有多个对象匹配,但是只返回第一个. Take(1):操作一个集合,可以有对个对象匹配,单只 ...

  2. LINQ to Entities 查询语法

    转自: http://www.cnblogs.com/asingna/archive/2013/01/28/2879595.html 实体框架(Entity Framework )是 ADO.NET  ...

  3. 浅谈sql 、linq、lambda 查询语句的区别

    浅谈sql .linq.lambda 查询语句的区别 LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量 ...

  4. xBIM 使用Linq 来优化查询

    目录 xBIM 应用与学习 (一) xBIM 应用与学习 (二) xBIM 基本的模型操作 xBIM 日志操作 XBIM 3D 墙壁案例 xBIM 格式之间转换 xBIM 使用Linq 来优化查询 x ...

  5. C# - LINQ 语言集成查询

    LINQ(Language Integrated Query) LINQ语言集成查询是一组用于C#语言的扩展.它允许编写C#代码对数据集进行查询,比如查询内存中的对象或查询远程数据库的表.利用linq ...

  6. C#编程(六十二)---------LINQ标准的查询操作符

    LINQ标准的查询操作符 首先我们来看一下LINQ的操作符,可根据查询操作符的操作”类型”进行分类,如把它们分成投影,限制,排序,联接,分组,串联,聚合,集合,生成,转换,元素,相等,量词,分割等. ...

  7. C#3.0新增功能09 LINQ 基础07 LINQ 中的查询语法和方法语法

    连载目录    [已更新最新开发文章,点击查看详细] 介绍性的语言集成查询 (LINQ) 文档中的大多数查询是使用 LINQ 声明性查询语法编写的.但是在编译代码时,查询语法必须转换为针对 .NET ...

  8. LINQ to Entities 查询注意事项

    1> 排序信息丢失 如果在排序操作之后执行了任何其他操作,则不能保证这些附加操作中会保留排序结果.这些操作包括 Select 和 Where 等.另外,采用表达式作为输入参数的 First 和 ...

  9. LINQ数据库连接对象制造工厂

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

随机推荐

  1. [学习笔记]node.js中的path.extname方法

    path.extname 返回path路径文件扩展名,如果path以 ‘.' 为结尾,将返回 ‘.',如果无扩展名 又 不以'.'结尾,将返回空值. path.extname('index.html' ...

  2. 200 from memory cache / from disk cache / 304 Not Modified 区别

    三者情况有什么区别和联系,什么情况下会发生200 from memory cache 或 200 from disk cache 或 304 Not Modified? 200 from memory ...

  3. Unity3D 之 C# 脚本

    引用类型 :class ,interface, 数组 , delegate , object ,string (object 是C#中所有类型,包括所有的值类型和引用类型的根类,string 类型是一 ...

  4. sql排名函数--四个

    1 row_number 2 rank 3 dense_rank 4 ntile 例子如下: select * into #MyTablefrom(select '语文' as 课程,70 as 成绩 ...

  5. UVa 1151 Buy or Build【最小生成树】

    题意:给出n个点的坐标,现在需要让这n个点连通,可以直接在点与点之间连边,花费为两点之间欧几里得距离的平方,也可以选购套餐,套餐中所含的点是相互连通的 问最少的花费 首先想kruskal算法中,被加入 ...

  6. JavaScript笔记(1)

    JS前导: ECMA欧洲计算机制造者协会 ECMA-262 (ES5规范) ECMA-404(Json规范) wsc定义HTML.CSS.DOM规范 计算机程序分为: cpu密集(用于计算) I/O密 ...

  7. HDU-2045 不容易系列之(3)—— LELE的RPG难题 找规律&递推

    题目链接:https://cn.vjudge.net/problem/HDU-2045 找规律 代码 #include <cstdio> long long num[51][2]; int ...

  8. CF949C Data Center Maintenance(建图+强联通分量)

    题意 有 n 个信息中心,第 i 个信息中心要在第 ti 个小时维护,维护期间信息不能被获得. 每个用户的数据都有两份备份,第 i 个用户的数据放在信息中心 c(i,1) 和 c(i,2). 现在要挑 ...

  9. 对比学习:《深度学习之Pytorch》《PyTorch深度学习实战》+代码

    PyTorch是一个基于Python的深度学习平台,该平台简单易用上手快,从计算机视觉.自然语言处理再到强化学习,PyTorch的功能强大,支持PyTorch的工具包有用于自然语言处理的Allen N ...

  10. 紫书 例题 10-17 UVa 1639(数学期望+分数处理+处理溢出)

    设当前有k个,那么也就是说拿到其他图案的可能是(n-k)/n 那么要拿到一个就要拿n/(n-k)次 所以答案就是n(1/n + 1/(n-1) ......1/2 + 1 / 1) 看起来很简单,但是 ...