Linq知识大全
select的源码public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);
class Program
{
static void Main(string[] args)
{
var customers = new[] {
new { CustomerID = 1, FirstName = "Orlando", LastName = "Gee",
CompanyName = "A Bike Store" },
new { CustomerID = 2, FirstName = "Keith", LastName = "Harris",
CompanyName = "Bike World" },
new { CustomerID = 3, FirstName = "Donna", LastName = "Carreras",
CompanyName = "A Bike Store" },
new { CustomerID = 4, FirstName = "Janet", LastName = "Gates",
CompanyName = "Fitness Hotel" },
new { CustomerID = 5, FirstName = "Lucy", LastName = "Harrington",
CompanyName = "Grand Industries" },
new { CustomerID = 6, FirstName = "David", LastName = "Liu",
CompanyName = "Bike World" },
new { CustomerID = 7, FirstName = "Donald", LastName = "Blanton",
CompanyName = "Grand Industries" },
new { CustomerID = 8, FirstName = "Jackie", LastName = "Blackwell",
CompanyName = "Fitness Hotel" },
new { CustomerID = 9, FirstName = "Elsa", LastName = "Leavitt",
CompanyName = "Grand Industries" },
new { CustomerID = 10, FirstName = "Eric", LastName = "Lang",
CompanyName = "Distant Inn" }
};
var addresses = new[] {
new { CompanyName = "A Bike Store", City = "New York", Country = "United States"},
new { CompanyName = "Bike World", City = "Chicago", Country = "United States"},
new { CompanyName = "Fitness Hotel", City = "Ottawa", Country = "Canada"},
new { CompanyName = "Grand Industries", City = "London",Country = "United Kingdom"},
new { CompanyName = "Distant Inn", City = "Tetbury", Country = "United Kingdom"}
};//查询数据
IEnumerable<string> customerFirstNames = customers.Select(cust => cust.FirstName);
//IEnumerable<string> customerFirstNames = from custs in customers select custs.FirstName;
foreach (string name in customerFirstNames)
{
Console.WriteLine(name);
}
Console.WriteLine("\r\n");IEnumerable<string> customerFullNames = customers.Select(cust => cust.FirstName + " " + cust.LastName);
//IEnumerable<string> customerFullNames = from custs in customers select custs.FirstName + " " + custs.LastName;
foreach (string name in customerFullNames)
{
Console.WriteLine(name);
}Console.WriteLine("\r\n");
var customerNames = customers.Select(cust => new { FirstName = cust.FirstName, LastName = cust.LastName });
//var customerNames = from custs in customers select new { FirstName = custs.FirstName, LastName = custs.LastName };
foreach (var name in customerNames)
{
Console.WriteLine(name);
}//数据筛选
Console.WriteLine("\r\n");
IEnumerable<string> usCompanies = addresses.Where(addr => String.Equals(addr.Country, "United States"))
.Select(usComp => usComp.CompanyName);
//IEnumerable<string> usCompanies = from adr in addresses
// where String.Equals(adr.Country, "United States")
// select adr.CompanyName;
foreach (string name in usCompanies)
{
Console.WriteLine(name);
}//数据排序
Console.WriteLine("\r\n");
IEnumerable<string> companyNames = addresses.OrderBy(addr => addr.CompanyName).Select(comp => comp.CompanyName);
//IEnumerable<string> companyNames = from adr in addresses orderby adr.CompanyName select adr.CompanyName;
foreach (string name in companyNames)
{
Console.WriteLine(name);
}Console.WriteLine("\r\n");
//数据分组---分组之后变成二维数组
var companiesGroupedByCountry = addresses.GroupBy(addrs => addrs.Country);
//var companiesGroupedByCountry = from adr in addresses group adr by adr.Country;
foreach (var companiesPerCountry in companiesGroupedByCountry)
{
Console.WriteLine("Country: {0}\t{1} companies",
companiesPerCountry.Key, companiesPerCountry.Count());
foreach (var companies in companiesPerCountry)
{
Console.WriteLine("\t{0}", companies.CompanyName);
}
}//数据统计
Console.WriteLine("\r\n");
int numberOfCountries = addresses.Select(addr => addr.Country).Distinct().Count();
//int numberOfCountries = (from adr in addresses select adr.Country).Distinct().Count();
Console.WriteLine("Number of countries: {0}", numberOfCountries);//联合查询--内联查询
Console.WriteLine("\r\n");
var citiesAndCustomers = customers.Select(c =>
new { c.FirstName, c.LastName, c.CompanyName })
.Join(addresses, custs => custs.CompanyName, addrs => addrs.CompanyName, (custs, addrs) => new { custs.FirstName, custs.LastName, addrs.Country });//var citiesAndCustomers = from cuts in customers
// join adr in addresses
// on cuts.CompanyName equals adr.CompanyName
// select new { cuts.FirstName, cuts.LastName, adr.Country };
foreach (var row in citiesAndCustomers)
{
Console.WriteLine(row);
}Console.ReadKey();
}
}
}private static void Orderby()
{
int[] intAry1 = { 4, 3, 556, 34, 6, 2, 67 };
var query1 = from var1 in intAry1
orderby var1 ascending
select var1;
query1.ToList().ForEach(p => Console.Write(p + Environment.NewLine));
var query2 = from var1 in intAry1
orderby var1 descending
select var1;
query2.ToList().ForEach(p => Console.Write(p + Environment.NewLine));
Console.WriteLine();
}private static void Join左外部连接()
{
int[] intAry1 = { 5, 15, 25, 30, 33, 40 };
int[] intAry2 = { 5, 20, 30, 30, 50, 70, 80 };
var query1 = from var1 in intAry1
join var2 in intAry2 on var1 equals var2 into var2Grp //左连接 右连接相反
from grp in var2Grp.DefaultIfEmpty()
select new { a = var1 != 0 ? var1 : 100, b = grp };
query1.ToList().ForEach(p => Console.Write("{0}:{1}", p.a, p.b + Environment.NewLine));
Console.WriteLine();
}private static void Join分组连接()
{
int[] intAry1 = { 5, 15, 25, 30, 33, 40 };
int[] intAry2 = { 5, 20, 30, 30, 50, 70, 80 };
var query1 = from var1 in intAry1
join var2 in intAry2 on var1 equals var2 into var2Grp
select new { a = var1, b = var2Grp };
foreach (var item in query1)
{
Console.Write("{0}:", item.a);
foreach (var o in item.b)
{
Console.Write("{0} ", o);
}
Console.WriteLine();
}
Console.WriteLine();
}private static void Join内部连接()
{
int[] intAry1 = { 5, 15, 25, 30, 33, 40 };
int[] intAry2 = { 5, 20, 30, 30, 50, 70, 80 };
var query1 = from var1 in intAry1
join var2 in intAry2 on var1 equals var2
select new { a = var1, b = var2 };
query1.ToList().ForEach(p => Console.Write(p.a + Environment.NewLine));
Console.WriteLine();
}
Linq知识大全的更多相关文章
- PHP知识大全
--------------------------------------------------------- PHP知识大全 ---------------------------------- ...
- python知识大全目录,想学的看过来!
Python总结篇——知识大全 python装饰器 PyCharm安装与配置,python的Hello World sort与sorted的区别及实例 我必须得告诉大家的MySQL优化 ...
- VC6.0调试知识大全
VC6.0调试知识大全 分类: C++ 2010-09-06 21:33 7080人阅读 评论(5) 收藏 举报 debuggingmfcfunctionmenumicrosoftdll My Not ...
- .Net知识大全(个人整理)
.Net知识大全 本章内容适用于对.NET有一定基础的或者是想通过本文章对.NET基础知识记不清楚的朋友,可以通过本文章进行回顾. 面试的时候可能也会遇到相应的题目,建议面试前进行回顾!!! 1.NE ...
- Linq 知识总结
一.说明: LINQ,语言集成查询(Language INtegrated Query)是一组用于c#和Visual Basic语言的扩展.它允许编写C#或者Visual Basic代码以查询数据库相 ...
- Linq 知识回顾
开篇语 在说LINQ之前必须先说说几个重要的C#语言特性 与LINQ有关的语言特性 隐式类型 (1)源起 在隐式类型出现之前, 我们在声明一个变量的时候, 总是要为一个变量指定他的类型 甚至在fore ...
- linq语法大全(转集)
Join操作符 适用场景:在我们表关系中有一对一关系,一对多关系,多对多关系等.对各个表之间的关系,就用这些实现对多个表的操作. 说明:在Join操作中,分别为Join(Join查询), Selec ...
- C#接口知识大全收藏
第一节 接口慨述 接口(interface)用来定义一种程序的协定.实现接口的类或者结构要与接口的定义严格一致.有了这个协定,就可以抛开编程语言的限制(理论上).接口可以从多个基接口继承,而类或结构可 ...
- 扫盲Linq知识
LINQ,语言集成查询(Language Integrated Query),是在.NET Framework 3.5 中出现的技术. 借助于LINQ技术,我们可以使用一种类似SQL的语法来查询任何形 ...
随机推荐
- CopyU!SW新版发布!
CopyU!SW新版发布,版本号为:2.1.412.213 主要更新内容如下: 此版本(2.1.412.213)主要作了如下更新: 1.修复了CopyU!SW版本中的运行模式规则的设定错 ...
- 微信.NET 微信开发 自己主动内容回复 ASP.NET C#代码
微信开发中,首先遇到的问题就是处理怎样接收和响应用户消息 , 本文将向大家介绍一下方法和关键的代码. 本文使用的接口库是 :https://github.com/chendong152/Weixin ...
- STM32 驱动12864液晶显示汉字、图片、画点、横线、竖线、斜线
我做本实验的软件平台为MDK软件,选用STM32VET6,12864液晶屏5v供电采用并行接法.之前本来想网上找一个现成的程序实验一下,但都没找到合适的,于是就自己编写了一个,最终可在12864液晶屏 ...
- Java SE学习之printf 日期转换符
本文是学习网络上的文章时的总结,感谢大家无私的分享. System.out.printf()方法能够对日期做处理输出. 相应列表 转换符 类型 举例 c 完整的日期和时间 Mon Feb 09 18: ...
- 一个IO的传奇一生 系列 存储之道
http://alanwu.blog.51cto.com/3652632/1286553
- ccmenu里的位置
ccmenu里的位置 ccctableviewcell内的元素不需要设置高度 调整buyitem内的元素的位置,可以通过一个item来调整. ccctableview.ccctableviewcell ...
- 【转】获取Sprite的实际Rect
判断点击是否点击在了一个精灵上, 其实就是判断一个点是否在一个矩形内. cocos2d-x的2.0.2版本可以使用CCRect的函数 bool CCRect::containsPoint(const ...
- 台湾书籍代购网址——2013-08-25 16
台湾书籍代购 博客来http://www.books.com.tw 三民http://www.sanmin.com.tw 诚品http://www.eslite.com 金石堂http://www.k ...
- Python开发笔记之正则表达式的使用
查找正则表达式 import re re_txt = re.compile(r'(\d)*.txt') m = re_txt.search(src) if not m == None: m.group ...
- 通过配置tomcat虚拟路径配置站点的静态资源
我们常常站点中会提供给用户上传文件.图片.视频或者诸如为了提高性能生成的静态文件等存储在站点应用中.但如果静态资源文件和项目文件在同一个目录下,当我们重新部署文件时,war重新解压会导致静态资源文件的 ...