Where - Simple 1

筛选出数值中大于5的元素

public void Linq1()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var lowNums =
from n in numbers
where n < 5
select n; Console.WriteLine("Numbers < 5:");
foreach (var x in lowNums)
{
Console.WriteLine(x);
}
}

Result

Numbers < 5:




0

Where - Simple 2

筛选出所有没有现货的产品

 public void Linq2()
{
List<Product> products = GetProductList(); var soldOutProducts =
from p in products
where p.UnitsInStock ==
select p; Console.WriteLine("Sold out products:");
foreach (var product in soldOutProducts)
{
Console.WriteLine("{0} is sold out!", product.ProductName);
}
}

Result

Sold out products: 
Chef Anton's Gumbo Mix is sold out! 
Alice Mutton is sold out! 
Thüringer Rostbratwurst is sold out! 
Gorgonzola Telino is sold out! 
Perth Pasties is sold out!

Where - Simple 3

筛选充所有产品中有现货并且价格大于3的产品

 public void Linq3()
{
List<Product> products = GetProductList(); var expensiveInStockProducts =
from p in products
where p.UnitsInStock > && p.UnitPrice > 3.00M
select p; Console.WriteLine("In-stock products that cost more than 3.00:");
foreach (var product in expensiveInStockProducts)
{
Console.WriteLine("{0} is in stock and costs more than 3.00.", product.ProductName);
}
}

Result

In-stock products that cost more than 3.00: 
Chai is in stock and costs more than 3.00. 
Chang is in stock and costs more than 3.00. 
Aniseed Syrup is in stock and costs more than 3.00. 
Chef Anton's Cajun Seasoning is in stock and costs more than 3.00. 
Grandma's Boysenberry Spread is in stock and costs more than 3.00. 
Uncle Bob's Organic Dried Pears is in stock and costs more than 3.00. 
Northwoods Cranberry Sauce is in stock and costs more than 3.00. 
Mishi Kobe Niku is in stock and costs more than 3.00. 
Ikura is in stock and costs more than 3.00. 
Queso Cabrales is in stock and costs more than 3.00. 
Queso Manchego La Pastora is in stock and costs more than 3.00. 
Konbu is in stock and costs more than 3.00. 
Tofu is in stock and costs more than 3.00. 

Where - Drilldown

筛选中在华盛顿的所有客户,显示出他们的订单单号和订单日期

 public void Linq4()
{
List<Customer> customers = GetCustomerList(); var waCustomers =
from c in customers
where c.Region == "WA"
select c; Console.WriteLine("Customers from Washington and their orders:");
foreach (var customer in waCustomers)
{
Console.WriteLine("Customer {0}: {1}", customer.CustomerID, customer.CompanyName);
foreach (var order in customer.Orders)
{
Console.WriteLine(" Order {0}: {1}", order.OrderID, order.OrderDate);
}
}
}
Result

Customers from Washington and their orders: 
Customer LAZYK: Lazy K Kountry Store

Order 10482: 3/21/1997 12:00:00 AM
Order 10545: 5/22/1997 12:00:00 AM 
Customer TRAIH: Trail's Head Gourmet Provisioners

Order 10574: 6/19/1997 12:00:00 AM
Order 10577: 6/23/1997 12:00:00 AM
Order 10822: 1/8/1998 12:00:00 AM 
Customer WHITC: White Clover Markets

Order 10269: 7/31/1996 12:00:00 AM
Order 10344: 11/1/1996 12:00:00 AM
Order 10469: 3/10/1997 12:00:00 AM
Order 10483: 3/24/1997 12:00:00 AM

Where - Indexed

筛选出字符串长度小于自己索引的元素

 public void Linq5()
{
string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var shortDigits = digits.Where((digit, index) => digit.Length < index); Console.WriteLine("Short digits:");
foreach (var d in shortDigits)
{
Console.WriteLine("The word {0} is shorter than its value.", d);
}
}

Result

Short digits: 
The word five is shorter than its value. 
The word six is shorter than its value. 
The word seven is shorter than its value. 
The word eight is shorter than its value. 
The word nine is shorter than its value.

 

Select - Simple 1

public void Linq6()
{
int[] numbers = { , , , , , , , , , }; var numsPlusOne =
from n in numbers
select n + ; Console.WriteLine("Numbers + 1:");
foreach (var i in numsPlusOne)
{
Console.WriteLine(i);
}
}
Result

Numbers + 1:
6
5
2
4
10
9
7
8
3
1

Select - Simple 2

public void Linq7()
{
List<Product> products = GetProductList(); var productNames =
from p in products
select p.ProductName; Console.WriteLine("Product Names:");
foreach (var productName in productNames)
{
Console.WriteLine(productName);
}
}

Result

Product Names:
Chai
Chang
Aniseed Syrup
Chef Anton's Cajun Seasoning
Chef Anton's Gumbo Mix
Grandma's Boysenberry Spread
Uncle Bob's Organic Dried Pears
Northwoods Cranberry Sauce
Mishi Kobe Niku
Ikura
Queso Cabrales
Queso Manchego La Pastora
Konbu
Tofu
Genen Shouyu
Pavlova
Alice Mutton
Carnarvon Tigers
Teatime Chocolate Biscuits
Sir Rodney's Marmalade

Select - Transformation

public void Linq8()
{
int[] numbers = { , , , , , , , , , };
string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var textNums =
from n in numbers
select strings[n]; Console.WriteLine("Number strings:");
foreach (var s in textNums)
{
Console.WriteLine(s);
}
}
Result

Number strings:
five
four
one
three
nine
eight
six
seven
two
zero

Select - Anonymous Types 1

public void Linq9()
{
string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" }; var upperLowerWords =
from w in words
select new { Upper = w.ToUpper(), Lower = w.ToLower() }; foreach (var ul in upperLowerWords)
{
Console.WriteLine("Uppercase: {0}, Lowercase: {1}", ul.Upper, ul.Lower);
}
}

Result

Uppercase: APPLE, Lowercase: apple
Uppercase: BLUEBERRY, Lowercase: blueberry
Uppercase: CHERRY, Lowercase: cherry

Select - Anonymous Types 2

public void Linq10()
{
int[] numbers = { , , , , , , , , , };
string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var digitOddEvens =
from n in numbers
select new { Digit = strings[n], Even = (n % == ) }; foreach (var d in digitOddEvens)
{
Console.WriteLine("The digit {0} is {1}.", d.Digit, d.Even ? "even" : "odd");
}
}

Result

The digit five is odd.
The digit four is even.
The digit one is odd.
The digit three is odd.
The digit nine is odd.
The digit eight is even.
The digit six is even.
The digit seven is odd.
The digit two is even.
The digit zero is even.

Select - Anonymous Types 3

public void Linq11()
{
List<Product> products = GetProductList(); var productInfos =
from p in products
select new { p.ProductName, p.Category, Price = p.UnitPrice }; Console.WriteLine("Product Info:");
foreach (var productInfo in productInfos)
{
Console.WriteLine("{0} is in the category {1} and costs {2} per unit.", productInfo.ProductName, productInfo.Category, productInfo.Price);
}
}

Result

Product Info:
Chai is in the category Beverages and costs 18.0000 per unit.
Chang is in the category Beverages and costs 19.0000 per unit.
Aniseed Syrup is in the category Condiments and costs 10.0000 per unit.
Chef Anton's Cajun Seasoning is in the category Condiments and costs 22.0000 per unit.
Chef Anton's Gumbo Mix is in the category Condiments and costs 21.3500 per unit.
Grandma's Boysenberry Spread is in the category Condiments and costs 25.0000 per unit.
Uncle Bob's Organic Dried Pears is in the category Produce and costs 30.0000 per unit.
Northwoods Cranberry Sauce is in the category Condiments and costs 40.0000 per unit.
Mishi Kobe Niku is in the category Meat/Poultry and costs 97.0000 per unit.
Ikura is in the category Seafood and costs 31.0000 per unit.
Queso Cabrales is in the category Dairy Products and costs 21.0000 per unit.
Queso Manchego La Pastora is in the category Dairy Products and costs 38.0000 per unit.
Konbu is in the category Seafood and costs 6.0000 per unit.
Tofu is in the category Produce and costs 23.2500 per unit.

Select - Indexed

public void Linq12()
{
int[] numbers = { , , , , , , , , , }; var numsInPlace = numbers.Select((num, index) => new { Num = num, InPlace = (num == index) }); Console.WriteLine("Number: In-place?");
foreach (var n in numsInPlace)
{
Console.WriteLine("{0}: {1}", n.Num, n.InPlace);
}
}

Result

Number: In-place?
5: False
4: False
1: False
3: True
9: False
8: False
6: True
7: True
2: False
0: False

Select - Filtered

public void Linq13()
{
int[] numbers = { , , , , , , , , , };
string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var lowNums =
from n in numbers
where n <
select digits[n]; Console.WriteLine("Numbers < 5:");
foreach (var num in lowNums)
{
Console.WriteLine(num);
}
}

Result

Numbers < 5:
four
one
three
two
zero

SelectMany - Compound from 1

public void Linq14()
{
int[] numbersA = { , , , , , , };
int[] numbersB = { , , , , }; var pairs =
from a in numbersA
from b in numbersB
where a < b
select new { a, b }; Console.WriteLine("Pairs where a < b:");
foreach (var pair in pairs)
{
Console.WriteLine("{0} is less than {1}", pair.a, pair.b);
}
}

Result

Pairs where a < b:
0 is less than 1
0 is less than 3
0 is less than 5
0 is less than 7
0 is less than 8
2 is less than 3
2 is less than 5
2 is less than 7
2 is less than 8
4 is less than 5
4 is less than 7
4 is less than 8
5 is less than 7
5 is less than 8
6 is less than 7
6 is less than 8

SelectMany - Compound from 2

public void Linq15()
{
List<Customer> customers = GetCustomerList(); var orders =
from c in customers
from o in c.Orders
where o.Total < 500.00M
select new { c.CustomerID, o.OrderID, o.Total }; ObjectDumper.Write(orders);
}

Result

CustomerID=ALFKI OrderID=10702 Total=330.00
CustomerID=ALFKI OrderID=10952 Total=471.20
CustomerID=ANATR OrderID=10308 Total=88.80
CustomerID=ANATR OrderID=10625 Total=479.75
CustomerID=ANATR OrderID=10759 Total=320.00
CustomerID=ANTON OrderID=10365 Total=403.20
CustomerID=ANTON OrderID=10682 Total=375.50
CustomerID=AROUT OrderID=10355 Total=480.00

SelectMany - Compound from 3

public void Linq16()
{
List<Customer> customers = GetCustomerList(); var orders =
from c in customers
from o in c.Orders
where o.OrderDate >= new DateTime(, , )
select new { c.CustomerID, o.OrderID, o.OrderDate }; ObjectDumper.Write(orders);
}

Result

CustomerID=ALFKI OrderID=10835 OrderDate=1/15/1998
CustomerID=ALFKI OrderID=10952 OrderDate=3/16/1998
CustomerID=ALFKI OrderID=11011 OrderDate=4/9/1998
CustomerID=ANATR OrderID=10926 OrderDate=3/4/1998
CustomerID=ANTON OrderID=10856 OrderDate=1/28/1998
CustomerID=AROUT OrderID=10864 OrderDate=2/2/1998
CustomerID=AROUT OrderID=10920 OrderDate=3/3/1998
CustomerID=AROUT OrderID=10953 OrderDate=3/16/1998
CustomerID=AROUT OrderID=11016 OrderDate=4/10/1998
CustomerID=BERGS OrderID=10837 OrderDate=1/16/1998

SelectMany - from Assignment

public void Linq17()
{
List<Customer> customers = GetCustomerList(); var orders =
from c in customers
from o in c.Orders
where o.Total >= 2000.0M
select new { c.CustomerID, o.OrderID, o.Total }; ObjectDumper.Write(orders);
}
Result

CustomerID=ANTON OrderID=10573 Total=2082.00
CustomerID=AROUT OrderID=10558 Total=2142.90
CustomerID=AROUT OrderID=10953 Total=4441.25
CustomerID=BERGS OrderID=10384 Total=2222.40
CustomerID=BERGS OrderID=10524 Total=3192.65
CustomerID=BERGS OrderID=10672 Total=3815.25
CustomerID=BERGS OrderID=10857 Total=2048.21
CustomerID=BLONP OrderID=10360 Total=7390.20
CustomerID=BOLID OrderID=10801 Total=3026.85
CustomerID=BONAP OrderID=10340 Total=2436.18
CustomerID=BONAP OrderID=10511 Total=2550.00
CustomerID=BOTTM OrderID=10742 Total=3118.00
CustomerID=BOTTM OrderID=10949 Total=4422.00
CustomerID=CHOPS OrderID=10519 Total=2314.20
CustomerID=CHOPS OrderID=10746 Total=2311.70
CustomerID=COMMI OrderID=10290 Total=2169.00

SelectMany - Multiple from

public void Linq18()
{
List<Customer> customers = GetCustomerList(); DateTime cutoffDate = new DateTime(, , ); var orders =
from c in customers
where c.Region == "WA"
from o in c.Orders
where o.OrderDate >= cutoffDate
select new { c.CustomerID, o.OrderID }; ObjectDumper.Write(orders);
}

Result

CustomerID=LAZYK OrderID=10482
CustomerID=LAZYK OrderID=10545
CustomerID=TRAIH OrderID=10574
CustomerID=TRAIH OrderID=10577
CustomerID=TRAIH OrderID=10822
CustomerID=WHITC OrderID=10469
CustomerID=WHITC OrderID=10483
CustomerID=WHITC OrderID=10504
CustomerID=WHITC OrderID=10596
CustomerID=WHITC OrderID=10693
CustomerID=WHITC OrderID=10696
CustomerID=WHITC OrderID=10723
CustomerID=WHITC OrderID=10740

SelectMany - Indexed

public void Linq19()
{
List<Customer> customers = GetCustomerList(); var customerOrders =
customers.SelectMany(
(cust, custIndex) =>
cust.Orders.Select(o => "Customer #" + (custIndex + ) +
" has an order with OrderID " + o.OrderID)); ObjectDumper.Write(customerOrders);
}

Result

Customer #1 has an order with OrderID 10643
Customer #1 has an order with OrderID 10692
Customer #1 has an order with OrderID 10702
Customer #1 has an order with OrderID 10835
Customer #1 has an order with OrderID 10952
Customer #1 has an order with OrderID 11011
Customer #2 has an order with OrderID 10308
Customer #2 has an order with OrderID 10625
Customer #2 has an order with OrderID 10759
Customer #2 has an order with OrderID 10926

...
Customer #90 has an order with OrderID 10248
Customer #90 has an order with OrderID 10615
Customer #90 has an order with OrderID 10673
Customer #90 has an order with OrderID 10695
Customer #90 has an order with OrderID 10873
Customer #90 has an order with OrderID 10879
Customer #90 has an order with OrderID 10910
Customer #90 has an order with OrderID 11005
Customer #91 has an order with OrderID 10374
Customer #91 has an order with OrderID 10611
Customer #91 has an order with OrderID 10792
Customer #91 has an order with OrderID 10870
Customer #91 has an order with OrderID 10906
Customer #91 has an order with OrderID 10998
Customer #91 has an order with OrderID 11044

 

101个Linq的例子的更多相关文章

  1. 101个linq例子

    FirstOrDefault - Simple public void Linq61() { int[] numbers = { }; int firstNumOrDefault = numbers. ...

  2. 101个Linq例子(40-60)

    GroupBy - Simple 2 public void Linq41() { string[] words = { "blueberry", "chimpanzee ...

  3. 101个LINQ示例,包含几乎全部操作

    Restriction Operators Where - Simple public void Linq1() { , , , , , , , , , }; var lowNums = from n ...

  4. Linq查询

    //Linq查询 List<A1> a1 = new List<A1>(); a1.Add(, Name = , Gender = true }); a1.Add(, Name ...

  5. linq简介

    语言集成查询(Language INtegrated Query,LINQ)是一项微软技术,新增一种自然查询的SQL语法到.NET Framework的编程语言中,可支持Visual Basic .N ...

  6. 口试Linq题

    LINQ to SQL与IQueryable 理解IQueryable的最简单方式就是,把它看作一个查询,在执行的时候,将会生成结果序列. LINQ to Object和LINQ to SQL有何区别 ...

  7. Linq 与 Lambda 简单使用

    //Lambda表达式详解 //int //List<int> numbers = new List<int> {1,2,3,4,5,6,7,8,9 }; //var n = ...

  8. LINQ(语言集成查询)

    LINQ,语言集成查询(Language Integrated Query)是一组用于c#和Visual Basic语言的扩展.它允许编写C#或者Visual Basic代码以查询数据库相同的方式操作 ...

  9. C# LINQ查询表达式用法对应Lambda表达式

    C#编程语言非常优美,我个人还是非常赞同的.特别是在学习一段时间C#后发现确实在它的语法和美观度来说确实要比其它编程语言强一些(也可能是由于VS编译器的加持)用起来非常舒服,而且对于C#我觉得他最优美 ...

随机推荐

  1. WCFRESTFul服务搭建及实现增删改查

    WCFRESTFul服务搭建及实现增删改查 RESTful Wcf是一种基于Http协议的服务架构风格,  RESTful 的服务通常是架构层面上的考虑. 因为它天生就具有很好的跨平台跨语言的集成能力 ...

  2. 两个80c51单片机之间怎样进行串行通信

    以前以为串行通信只能是单片机和PC机之间进行通信,昨天无意之中看到一个程序,是单片机和单片机之间进行通信..这小东西真是神奇啊!昨天弄了很长时间没弄出来,今天在大神的帮助下终于拨开云雾见天日了. 案例 ...

  3. GestureDetector学习之左右滑动,上下滑动屏幕切换页面

    要实现滑屏等触发事件,视情况而定: 如果实现的触屏或者手势效果较多,则使用第一种方法,实现OnGestureListener 接口(参考OnGestureListener): 如果只是实现较少的效果, ...

  4. sqlserver2005级联删除

    在你建表,建主外键的时候,在下面有几个选项,有一个是级联删除和一个级联更新,勾选上就可以了

  5. 初窥Linux 之 数据流重定向

    一.什么是数据流重定向 在说数据流重定向之前,先来说说数据流的概念吧.数据流分为三种:标准输入(stdin),标准输出(stdout)和标准错误输出(stderr). 简单来说,标准输出指的是命令执行 ...

  6. 定义文件XML——从简单开始

    本文纯属个人见解,是对前面学习的总结,如有描述不正确的地方还请高手指正~ 通过看XML讲授的视频,算是对XML有了简略的认识,原本不盘算写这篇博客,但无法原来视频讲授的内容就少,再不踊跃写些东西,过不 ...

  7. OGG学习笔记02-单向复制配置实例

    OGG学习笔记02-单向复制配置实例 实验环境: 源端:192.168.1.30,Oracle 10.2.0.5 单实例 目标端:192.168.1.31,Oracle 10.2.0.5 单实例 1. ...

  8. MVC源码解析 - Http Pipeline 解析(下)

    接上一篇, 我在 HttpModule 的Init方法中, 添加了自己的事件, 在Pipeline里, 就会把握注册的事件给执行了. 那么Pipeline是如何执行并且按照什么顺序执行的呢? 现在我们 ...

  9. iOS开发-OC语言 (五)字典

    字典 主要知识点: 1.NSDictionary 类 2.NSMutableDictionary 类 3.了解NSMutableDictionary 与 NSDictionary 的继承关系 4.补充 ...

  10. 详解一下网络广告cpc、cpm、cpl、cpa、cps、cpr的计费方法是什么

    CPC(Cost per click)按照 广告 点击数 计费 ,限定一个IP在24小时内只能点击一次.CPM(Cost per mille)按照广告显示次数来计算广告费,可在短时间内为 网站 带来巨 ...