Linq select 语法
文档:https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
1.可以对查询出来的结果做一些转换,下面的例子在数组中查找以"B"开头的名字,然后全部转成小写输出
string[] names = { "Jack", "Bob", "Bill", "Catty", "Willam" };
var rs = from n in names
where n.StartsWith("B")
select n.ToLower();
foreach (var item in rs)
{
Response.Write(item+"<br />");
}
2.返回匿名类型,只返回需要的信息
var users = from c in UserList
where c.Mobile.StartsWith("135")
select new
{
Name = c.Name,
Contact = c.Address + " " + c.Mobile
};
3.使用原数组,产生两组新数组
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);
}
}
4.判断数字是否与他的下标一致
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var numsInPlace = numbers.Select((num, index) => new { Num = num, InPlace = (num == index) }); Console.WriteLine("Number: In-place?");
foreach (var n in numsInPlace)
{
Response.Write(string.Format("{0}: {1}<br />", n.Num, n.InPlace));
}
5.获得A小于B的所有可能的集合,返回 拿A中的每个元素和B中的每个元素对比的结果
int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
int[] numbersB = { 1, 3, 5, 7, 8 }; 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);
}
6.从一个集合的子集中过滤,金额小于500的订单
List<Customer> customers = GetCustomerList(); var orders =
from c in customers
from o in c.Orders
where o.Total < 500.00
select new { c.CustomerID, o.OrderID, o.Total };
7.查询所有大于指定日期的用户订单
List<Customer> customers = GetCustomerList();
var orders =
from c in customers
from o in c.Orders
where o.OrderDate >= new DateTime(1998, 1, 1)
select new { c.CustomerID, o.OrderID, o.OrderDate }; 延伸:
List<Customer> customers = GetCustomerList();
DateTime cutoffDate = new DateTime(1997, 1, 1);
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 };
8.代替二重for循环
参考:http://www.cnblogs.com/manupstairs/archive/2012/11/27/2790114.html
http://www.cnblogs.com/lyout/archive/2012/11/28/2792622.html
List<Customer> customers = GetCustomerList(); var customerOrders =
customers.SelectMany(
(cust, custIndex) =>
cust.Orders.Select(o => "Customer #" + (custIndex + 1) + " has an order with OrderID " + o.OrderID));
9.Cross join 交叉连接查询
public void Linq102()
{ string[] categories = new string[]{
"Beverages",
"Condiments",
"Vegetables",
"Dairy Products",
"Seafood" }; List<Product> products = GetProductList(); var q =
from c in categories
join p in products on c equals p.Category
select new { Category = c, p.ProductName }; foreach (var v in q)
{
Console.WriteLine(v.ProductName + ": " + v.Category);
}
}
Result:
Chai: Beverages
Chang: Beverages
Guaraná Fantástica: Beverages
Sasquatch Ale: Beverages
Steeleye Stout: Beverages
Côte de Blaye: Beverages
Chartreuse verte: Beverages
Ipoh Coffee: Beverages
10. Group join 分组查询;将数据按照不同的类型划分,归入到不同的组中。
public void Linq103()
{
string[] categories = new string[]{
"Beverages",
"Condiments",
"Vegetables",
"Dairy Products",
"Seafood" }; List<Product> products = GetProductList(); var q =
from c in categories
join p in products on c equals p.Category into ps
select new { Category = c, Products = ps }; foreach (var v in q)
{
Console.WriteLine("组"+v.Category + ":");
foreach (var p in v.Products)
{
Console.WriteLine(" " + p.ProductName);
}
}
}
Result: 组Beverages:
Chai
Chang 组Condiments:
Aniseed Syrup
Chef Anton's Cajun Seasoning
Chef Anton's Gumbo Mix 组Vegetables:
Dairy Products:
Queso Cabrales
11.Left Outer Join 左外连接查询,如果连接表没有数据,这使用空值/默认值填充
public void Linq105()
{
string[] categories = new string[]{
"Beverages",
"Condiments",
"Vegetables",
"Dairy Products",
"Seafood" }; List<Product> products = GetProductList(); var q =
from c in categories
join p in products on c equals p.Category into ps
from p in ps.DefaultIfEmpty()
select new { Category = c, ProductName = p == null ? "(No products)" : p.ProductName }; foreach (var v in q)
{
Console.WriteLine(v.ProductName + ": " + v.Category);
}
}
Result:
Chai: Beverages
Aniseed Syrup: Condiments
(No products): Vegetables
Queso Cabrales: Dairy Products
Konbu: Seafood
Linq select 语法的更多相关文章
- .NET LINQ查询语法与方法语法
LINQ 查询语法与方法语法 通过使用 C# 3.0 中引入的声明性查询语法,介绍性 LINQ 文档中的多数查询都被编写为查询表达式. 但是,.NET 公共语言运行时 (CLR) 本身并不具 ...
- SQL,LINQ,Lambda语法对照图(转载)
如果你熟悉SQL语句,当使用LINQ时,会有似曾相识的感觉.但又略有不同.下面是SQL和LINQ,Lambda语法对照图 SQL LINQ Lambda SELECT * FROM HumanReso ...
- LINQ之路 4:LINQ方法语法
书写LINQ查询时又两种语法可供选择:方法语法(Fluent Syntax)和查询语法(Query Expression). LINQ方法语法是非常灵活和重要的,我们在这里将描述使用链接查询运算符的方 ...
- linq的语法和案例
本篇逐一介绍linq各个关键字的用法(from,select,group,into等),本篇所有的案例都是用linqpad来完成的(官方地址:http://www.linqpad.net/),建议想学 ...
- LINQ教程二:LINQ操作语法
LINQ查询时有两种语法可供选择:查询表达式语法(Query Expression)和方法语法(Fluent Syntax). 一.查询表达式语法 查询表达式语法是一种更接近SQL语法的查询方式. L ...
- linq查询语法和方法-簡單用法
來自:http://www.cnblogs.com/knowledgesea/p/3897665.html 1.简单的linq语法 //1 var ss = from r in db.Am_recPr ...
- 四:MVC之LINQ方法语法
linq 查询 有两种语法 ,前面我们说了一种,接下来说方法语法(我读着一直很绕口) 查询语法,方法语法 ------------------------以下文字都是复制-------------- ...
- 二:MVC之LINQ查询语法
LINQ(Language Integrated Query)语言集成查询是一组用于c#和Visual Basic语言的扩展.它允许编写C#或者Visual Basic代码以操作内存数据的方式,查询数 ...
- MySQL(九)之数据表的查询详解(SELECT语法)一
这一篇是MySQL中的重点也是相对于MySQL中比较难得地方,个人觉得要好好的去归类,并多去练一下题目.MySQL的查询也是在笔试中必有的题目.希望我的这篇博客能帮助到大家! 重感冒下的我,很难受!k ...
随机推荐
- java的多态性(二)
2013-10-16 19:44 9364人阅读 评论(25) 收藏 举报 分类: [JAVA开发]-----Java提高篇(36) 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录 ...
- CYQ.Data 批量添加数据性能测试(每秒千、万)---003
原文地址:https://www.cnblogs.com/cyq1162/p/3216267.html 今天有网友火晋地同学进了CYQ.Data官方群了,他正在折腾了一个各大ORM性能测试的比较的软件 ...
- springTask任务调度
1什么是任务调度 在企业级应用中,经常会制定一些“计划任务”,即在某个时间点做某件事情,核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作.常见的任务调度框架有Quartz和Sprin ...
- 手机端移动端的选择框mobileSelect.js使用
手机端移动端的选择框mobileSelect.js使用 文件地址:https://github.com/onlyhom/mobileSelect.js 请感兴趣的自行下载 使用过程 1 引入标签 &l ...
- PHP 程序员学数据结构与算法之《栈》
“要成高手,必练此功”. 要成为优秀的程序员,数据结构和算法是必修的内容.而现在的Web程序员使用传统算法和数据结构都比较少,因为很多算法都是包装好的,不用我们去操心具体的实现细节,如PHP的取栈 ...
- 拒绝网页被 iframe 嵌套
在响应头里加一个X-Frame-Options DENY:浏览器拒绝当前页面加载任何Frame页面 SAMEORIGIN:frame页面的地址只能为同源域名下的页面 ALLOW-FROM origin ...
- jsp访问java变量
jsp页面中javascript访问 java的变量 <%= JAVA变量名%> jsp中嵌入java代码<%java代码%> --变量<% String JAVASOu ...
- JavaScript加法
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- 键值集合List转换成datatable
/// <summary> /// 键值集合List转换成datatable /// </summary> /// <param name="data" ...
- 【转】Java自学之路——by马士兵
作者:马士兵老师 JAVA自学之路 一:学会选择 为了就业,不少同学参加各种各样的培训. 决心做软件的,大多数人选的是java,或是.net,也有一些选择了手机.嵌入式.游戏.3G.测试等. 那么究竟 ...