博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
重视Linq技术_6
阅读量:7294 次
发布时间:2019-06-30

本文共 3206 字,大约阅读时间需要 10 分钟。

Filtering

//1、Simple Local Filter

string[] names = {
"Tom",
"Dick",
"Harry",
"Mary",
"Jay" };
IEnumerable<
string> query =
    names.Where (name => name.EndsWith (
"y"));
query.Dump (
"In lambda syntax");
query =
   
from n
in names
   
where n.EndsWith (
"y")
   
select n;
query.Dump (
"In query comprehension syntax");

//2、Multiple Where Clauses

var names =
new[] {
"Tom",
"Dick",
"Harry",
"Mary",
"Jay" }.AsQueryable();
var query =
   
from n
in names
   
where n.Length > 3
   
let u = n.ToUpper()
   
where u.EndsWith (
"Y")
   
select u;
query.Dump();
//3、Indexed Filtering
string[] names = {
"Tom",
"Dick",
"Harry",
"Mary",
"Jay" };
names.Where ((n, i) => i % 2 == 0).Dump (
"Skipping every second element");

//4、Contains and LIKE with LINQ to SQL

Customers.Where (c => c.Name.Contains (
"a")) 
    .Dump (
"Notice the SQL translation uses LIKE");
Customers.Where (c => c.Name.StartsWith (
"J")) 
    .Dump (
"StartsWith and EndsWith also translate to LIKE");
Customers.Where (c => SqlMethods.Like (c.Name,
"_ar%y"))
    .Dump (
"A more complex use of LIKE");
// The following string functions all have translations to SQL:
// StartsWith, EndsWith, Contains, Length, IndexOf,
// Trim, Substring, Insert, Remove, Replace,
// ToUpper, ToLower

 

//5、IN and NOT IN with LINQ to SQL

string[] chosenOnes = {
"Tom",
"Jay" };
Customers.Where (c => chosenOnes.Contains (c.Name))
    .Dump (
"This translates to SQL WHERE ... IN");
Customers.Where (c => !chosenOnes.Contains (c.Name))
    .Dump (
"This translates to SQL WHERE NOT ... IN");

//6、Extra - Where-based Subqueries

 

 

var names =
new[] {
"Tom",
"Dick",
"Harry",
"Mary",
"Jay" }.AsQueryable();
(
   
from n
in names
   
where n.Length == names.Min (n2 => n2.Length)
   
select n
)
.Dump (
"Basic subquery");
// The same principle works well in LINQ to SQL:
(
   
from c
in Customers
   
where c.Name.Length == Customers.Min (c2 => c2.Name.Length)
   
select c
)
.Dump (
"Basic subquery, LINQ to SQL");
// We can construct similar subqueries across association properties:
(
   
from c
in Customers
   
where c.Purchases.Any (p => p.Price > 1000)
   
select c
)
.Dump (
"Customers who have purchased at least one item > $1000");

//6、Extra - Where-based Subqueries with let

// The let keyword in comprehension queries comes in useful with subqueries: it lets
// you re-use the subquery in the projection:
from c
in Customers
let highValuePurchases = c.Purchases.Where (p => p.Price > 1000)
where highValuePurchases.Any()
select 
new
{
    c.Name,
    highValuePurchases
}

image

//7、Take and Skip

// reflection (using LINQ, of course!) The following query extracts all type

// names in the mscorlib assembly:

string[] typeNames =
    (
from t
in 
typeof (
int).Assembly.GetTypes()
select t.Name).ToArray();
typeNames
    .Where (t => t.Contains (
"Exception"))
    .OrderBy (t => t)
    .Take (20)
    .Dump (
"The first 20 matches");
typeNames
    .Where   (t => t.Contains (
"Exception"))
    .OrderBy (t => t)
    .Skip (20)
    .Take (20)
    .Dump (
"Matches 21 through 40");
 
//8、TakeWhile and SkipWhile
int[] numbers = { 3, 5, 2, 234, 4, 1 };
numbers.TakeWhile (n => n < 100).Dump (
"TakeWhile");
numbers.SkipWhile (n => n < 100).Dump (
"SkipWhile");
image
//9、Distinct

"HelloWorld".Distinct()

image

Purchases.Select (p => p.Description).Distinct()

image 

转载于:https://www.cnblogs.com/sanic/archive/2012/03/05/2380960.html

你可能感兴趣的文章
由网络引起的打印故障和邮件问题
查看>>
xml相关
查看>>
如何将App从一个账号迁移到另一个账号?
查看>>
linux系统修改字符集
查看>>
phantomjs-截图比例
查看>>
javascript for of
查看>>
EF6 秘籍 2th:实体数据建模基础 (十二)使用条件过滤对象集合
查看>>
30天了解30种技术系列---(1)现代web应用服务器-Express.js
查看>>
某android平板项目开发笔记----aChartEngine图表显示(2)
查看>>
マクロ使用基準
查看>>
将博客搬至CSDN
查看>>
如何mac下安装virtual,并识别usb接口
查看>>
Ansible批量部署zabbix-agent
查看>>
使用PowerShell对比两个服务器系统进程和软件清单
查看>>
线程池的概述和使用学习笔记
查看>>
oracle基础之日志系列
查看>>
【NetApp】移动磁盘柜到一个新的控制器
查看>>
实在太伟大了,感谢楼主共享深度爬取和广度爬取
查看>>
crontab调用python时出现ImportError: No module named XXX的问题
查看>>
方正智睿NoSQL数据库总体介绍
查看>>