linq用法
2015-08-04 12:03
302 查看
1.什么是linq
这个就不解释了~~
两种查找方法:查询表达式和方法语法
查询表达式关键字:Where、Select、SelectMany、OrderBy、ThenBy、OrderByDescending、ThenByDescending、GroupBy、Join、GroupJoin
查询表达式语法:
from [type] id in source
[join [type] id in source on expr equals expr [into subGroup]]
[ from [type] id in source | let id = expr | where condition]
[ orderby ordering,ordering,ordering...]
select expr | group expr by key
[into id query]
2.使用linq 的两个步骤
创建查询形式
执行查询
3. 注意点
所有的linq查询都是以from 开始 以select 或者group 结束
查询形式确定后 改变查询的数据原 结果随之改变
4. 简单例子
int[] arr = { 1, 2, -3, -4, 5, 6, 7, 8 };
var posNums = from num in arr // num: 范围变量
where num > 0 // 查询条件 (推断) 必须是bool 值 过滤数据
select num; -----------------------> 创建查询形式
StringBuilder sb = new StringBuilder();
foreach (var item in posNums) -------------------------> 执行查询
{
sb.AppendFormat("{0},", item);
}
MessageBox.Show(sb.ToString());
5. 知识点
可以返回对范围变量的操作结果
var posNums = from num in arr
where num > 0
select num*2;
可以在中间定义一个变量
var posNums = from num in arr
let index = 2 //定义一个变量
where num > 0
select num*index ;
分组:
string[] arr = { ".net", "www.baidu.com", "www.google.com", "www.bing.com", "ee" };
var aa = from a in arr
let index = a.LastIndexOf(".") // 可以在中间定义一个变量
where index != -1
group a by a.Substring(index);
foreach (var item in aa) // 遍历组
{
StringBuilder sb = new StringBuilder();
sb.Length = 0;
sb.AppendFormat("{0}:", item.Key);
sb.AppendLine();
foreach (var i in item) // 遍历组中的元素
{
sb.AppendFormat("{0},", i);
}
MessageBox.Show(sb.ToString());
}
操作符使用示例:
http://www.aizhengli.com/yubinfeng-net-mianxiangduixiangjichu/776/LINQ%E4%BD%BF%E7%94%A8.html
这个就不解释了~~
两种查找方法:查询表达式和方法语法
查询表达式关键字:Where、Select、SelectMany、OrderBy、ThenBy、OrderByDescending、ThenByDescending、GroupBy、Join、GroupJoin
查询表达式语法:
from [type] id in source
[join [type] id in source on expr equals expr [into subGroup]]
[ from [type] id in source | let id = expr | where condition]
[ orderby ordering,ordering,ordering...]
select expr | group expr by key
[into id query]
2.使用linq 的两个步骤
创建查询形式
执行查询
3. 注意点
所有的linq查询都是以from 开始 以select 或者group 结束
查询形式确定后 改变查询的数据原 结果随之改变
4. 简单例子
int[] arr = { 1, 2, -3, -4, 5, 6, 7, 8 };
var posNums = from num in arr // num: 范围变量
where num > 0 // 查询条件 (推断) 必须是bool 值 过滤数据
select num; -----------------------> 创建查询形式
StringBuilder sb = new StringBuilder();
foreach (var item in posNums) -------------------------> 执行查询
{
sb.AppendFormat("{0},", item);
}
MessageBox.Show(sb.ToString());
5. 知识点
可以返回对范围变量的操作结果
var posNums = from num in arr
where num > 0
select num*2;
可以在中间定义一个变量
var posNums = from num in arr
let index = 2 //定义一个变量
where num > 0
select num*index ;
分组:
string[] arr = { ".net", "www.baidu.com", "www.google.com", "www.bing.com", "ee" };
var aa = from a in arr
let index = a.LastIndexOf(".") // 可以在中间定义一个变量
where index != -1
group a by a.Substring(index);
foreach (var item in aa) // 遍历组
{
StringBuilder sb = new StringBuilder();
sb.Length = 0;
sb.AppendFormat("{0}:", item.Key);
sb.AppendLine();
foreach (var i in item) // 遍历组中的元素
{
sb.AppendFormat("{0},", i);
}
MessageBox.Show(sb.ToString());
}
操作符使用示例:
http://www.aizhengli.com/yubinfeng-net-mianxiangduixiangjichu/776/LINQ%E4%BD%BF%E7%94%A8.html
相关文章推荐
- cloudstack_template_snapshot_volume
- [Ptrace]Linux内存替换(三)运行控制
- 初学者很实用:数据库连接池之c3p0入门
- Virtualbox虚拟机网络配置(NAT + Host-only - Bridged)
- iOS中NSArray, NSSet, NSDictionary的使用
- Codeforces 148 D Bag of mice(概率DP)
- Couldn't get lock for %t/vertx.log
- sql中时间的比较方法
- Spring AOP 使用注解为API引入新功能
- iOS开发之理解iOS中的MVC设计模式
- iOS开发利器-CocoaPods安装和使用教程
- .net连接sqlite数据库
- ZooKeeper-3.3.4集群安装配置
- Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源 方法
- [HDU 2222] Keywords Search AC自动机
- ADF Generator for Dynamic ADF BC and ADF UI
- 设计模式 - 工厂模式
- MyBatis3整合Spring3、SpringMVC3
- 非类型模板参数
- 来,接招!!! (八个Android项目源码)