Linq复杂对象查询
复杂的查询对象,
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复杂对象查询的更多相关文章
- 转linq中的Single()、First()、Take(1) LINQ 标准的查询操作符 排序 orderby、thenby、Take
Single():操作一个集合,同时强要求只有一个对象匹配,并返回这一个. First():操作一个集合,可以有多个对象匹配,但是只返回第一个. Take(1):操作一个集合,可以有对个对象匹配,单只 ...
- LINQ to Entities 查询语法
转自: http://www.cnblogs.com/asingna/archive/2013/01/28/2879595.html 实体框架(Entity Framework )是 ADO.NET ...
- 浅谈sql 、linq、lambda 查询语句的区别
浅谈sql .linq.lambda 查询语句的区别 LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量 ...
- xBIM 使用Linq 来优化查询
目录 xBIM 应用与学习 (一) xBIM 应用与学习 (二) xBIM 基本的模型操作 xBIM 日志操作 XBIM 3D 墙壁案例 xBIM 格式之间转换 xBIM 使用Linq 来优化查询 x ...
- C# - LINQ 语言集成查询
LINQ(Language Integrated Query) LINQ语言集成查询是一组用于C#语言的扩展.它允许编写C#代码对数据集进行查询,比如查询内存中的对象或查询远程数据库的表.利用linq ...
- C#编程(六十二)---------LINQ标准的查询操作符
LINQ标准的查询操作符 首先我们来看一下LINQ的操作符,可根据查询操作符的操作”类型”进行分类,如把它们分成投影,限制,排序,联接,分组,串联,聚合,集合,生成,转换,元素,相等,量词,分割等. ...
- C#3.0新增功能09 LINQ 基础07 LINQ 中的查询语法和方法语法
连载目录 [已更新最新开发文章,点击查看详细] 介绍性的语言集成查询 (LINQ) 文档中的大多数查询是使用 LINQ 声明性查询语法编写的.但是在编译代码时,查询语法必须转换为针对 .NET ...
- LINQ to Entities 查询注意事项
1> 排序信息丢失 如果在排序操作之后执行了任何其他操作,则不能保证这些附加操作中会保留排序结果.这些操作包括 Select 和 Where 等.另外,采用表达式作为输入参数的 First 和 ...
- LINQ数据库连接对象制造工厂
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
随机推荐
- Traversal with a for loop
A lot of computations involve processing a string one character at a time. Often they start at the b ...
- POJ 3661 DP
题意: 思路: i表示到了i,j表示疲劳度为j f[i][j]表示能跑的最大距离 f[i][j]=f[i-1][j-1]+a[i] if(i-j>=0)f[i][0]=max(f[i][0],f ...
- ubuntu14.04下snort的安装(官方文档安装)(图文详解)
不多说,直接上干货! 最近为了科研,需要安装和使用Snort. snort的官网 https://www.snort.org/ Snort作为一款优秀的开源主机入侵检测系统,在windows和Linu ...
- Java类和对象11
首先,编写一个类ChongZai,该类中有3个重载的方法void print():其次,再编写一个主类来测试ChongZai类的功能. public class ChongZai { public v ...
- PL/SQL恢复默认窗口样式
对于初学者来讲,使用PL/SQL时会不小心将窗体关闭,如下图 2. 怎么恢复呢?其实很简单 3. 搞定 转自:http://blog.csdn.net/hello_word2/article/deta ...
- P3157 [CQOI2011]动态逆序对 CDQ分治
一道CDQ分治模板题简单来说,这道题是三维数点对于离线的二维数点,我们再熟悉不过:利用坐标的单调递增性,先按更坐标排序,再按纵坐标排序更新和查询时都直接调用纵坐标.实际上,我们是通过排序将二维中的一维 ...
- CF19E Fairy(树上差分)
题目描述 很久很久以前,有一个仙女叫做A.有一天一个少年B找到她,并且请求她预测他的未来.仙女看着她的水晶球,说这位少年不久将遇见世界上最美丽的公主,并且将迎娶她为妻.然后仙女在一张纸上画了n个点,并 ...
- Linux学习,部署django项目到服务器,及安装python,uwsgi等
开启网络 vi /etc/sysconfig/network-script/ifcfg-eth0 onboot=yes 退出保存 service network restart ping www.ba ...
- Delayer 基于 Redis 的延迟消息队列中间件
Delayer 基于 Redis 的延迟消息队列中间件,采用 Golang 开发,支持 PHP.Golang 等多种语言客户端. 参考 有赞延迟队列设计 中的部分设计,优化后实现. 项目链接:http ...
- 二 JDK + mysql + yum + rpm
如果系统环境崩溃. 调用/usr/bin/vim /etc/profile 1 网络搭建 2 host配置 3 SSH无密码登录 4 rpm 安装 yum install ...