Linq的基本用用法
Linq 的基本用法: Sort , OrderBy, Skip,Take,Where,Compare,Join,Distinct ,InsertRange 等关键词
Select用法 var selectedItems = from item in items where item.ParentID == parentID orderby item.SortIndex descending ,item.Name ascending select item;
0.1 where : var list=collection.Where(t => (txtCustomerName.Text.Trim().Length == 0 || t.ClientName.ToUpper().IndexOf(txtCustomerName.Text.Trim().ToUpper()) >= 0)); // 根据条件查询,如果 txtCustomerName中有值 则匹配collection中的ClientName 是否包含这个txtCustomerName 的值
10.1 LInq 递归实现
定义 function private BuildExpression(IEnumberable<string> enumberableList){...}
return factory =>
{ //递归实现的function
BuildExpression(factory);
};
一、 LinQ To Object 主要使用在 一些 array ,list,collection ,IEnumerable 等数组上面,
var list=new List<T>(); var list2=new List<T>();
1.01 list.Select(t=>t.CreateDate).SeperateToString(","); //select createdate 字段集合并组成string 返回
1.02 list.Select(t=>new DataEntity{Name=t.Name,Value=t.Value}); //select TypeOf(list) 类型中的某些字段到新的 DataEntity 实例中
1.11 list.OrderBy(entity=>entity.CreateDate); //entity 表示 T的一个实例,按照 createdate 顺序排列,反之 则使用 listOrderByDescing
1.12 selectedItems.OrderBy(entity => entity.SortIndex).ThenBy(entity => entity.name); // 多个字段排序
1.2 list.Sort( (x, y) => StringComparer.CurrentCultureIgnoreCase.Compare(x.CreateDate,y.CreateDate)); //x,y 表示 T的一个实例, x 在y前面表示 顺序排列,如果变为Compare(y.CreateDate,x.CreateDate)表示倒序排列
1.3 list.Skip(count) //count 表示 跳过count 个数据 处理分页可以使用 list.Skip((page-1)*pageSize).Take(pageSize);
1.4 list.Take(count) //count 表示 选取count 个数据
实例1(按照createdate 排序,并选取前 n个T 类型的集合):
list.OrderBy(entity==>entity.CreateDate).Take(n).ToList<T>();
1.5 list.Distinct(); //删除重复项,list 必须为一维数据组
1.6 list.Count(); //list 数字的记录条数
1.6.1 list.Count(item=>item.Name=='test') //查询list中 name 为 test 的记录条数
1.7 list.Sum(); //合计,list 必须为一维数组
1.7.1 list.Sum(item=>item.Quantity); //合计,合计list中的quantity 字段
1.8 list.Min(); //list 中的最小值或记录
1.8.1 list.Select(item=>item.Quantity).Min() //或者list中 数量最少的记录
1.8.2 list.Min(item=>item.Quantity) //或者list中 数量最少的记录
1.8.3 变相实现list min 对 两个属性进行比较
list.Orderby(t=>t.Quantity).thenBy(t=>t.Price).FirstOrDefault();
1.8.10 list.InsertRange(0,list2) //在list的指定位置插入list 2
1.9 list.ForEach(item=>item.Quantity+=1) //每个item的quantity 加1
2.0 list.Concat (list2) // 两个同类型的list ,list2 组合起来,并且不去除相同记录,顺序为 list2 追加到list 后面
2.1 list.Union(list2) //两个同类型的list ,list2 组合, 去除相同记录,并追加到list 后面
list.Union(list2,, new LambdaComparer<string>((a, b) => a == b))
2.2 list.Intersect (list2) //取相交项,取list1,list2 相同项,并且后面可以加compare 条件
2.2 list.Except(list2) //从list中选择 除了 list2中含有的其他所有数据
2.3 list.Aggregate((x,y)=>xyexpression) //聚合函数,将 list中数据 分别进行 聚合
比如 : var list = new List<string>(){"1","12","13","14","15","19","111","121","","23"};
var strReturn = list.Aggregate("return ", (x, y) => (string.IsNullOrEmpty(y) ? x : x + y + " && "));
strReturn = strReturn.Substring(0, strReturn.Length - 3) + ";";
结果为: return 1 && 12 && 13 && 14 && 15 && 19 && 111 && 121 && 23 ;
例子的功能也可以简化为 list.Join("&&")
2.4 list.Join(stringSeperator) 将list中item 使用 stringSeperator 连接起来,如上面的例子
一. 组合应用
以下应用实现了 多个node 和 connector 之间的关联. node 可能没有与connector 相连接, 但是connector 必须与node 连接
实现 取出所有connector , 并取出有连接线的node 和 没有连接线的node
var listFlowActions = new List<FlowAction>();
var connectors = flowActions.Where(i => i.ActionType == MongoConstants.FlowActions.Connector
&& IsFlowActionExists(i.SourceFlowActionId)
&& IsFlowActionExists(i.TargetFlowActionId)).OrderBy(connection => connection.Index);
var allNodes = flowActions.Where(i => i.ActionType != MongoConstants.FlowActions.Connector);
var connectedNodes = new List<FlowAction>();
connectors.Each(t => connectedNodes.Add(allNodes.Where(i => i.Id == t.SourceFlowActionId).FirstOrDefault()))
.Each(t=>connectedNodes.Add(allNodes.Where(i=>i.Id==t.TargetFlowActionId).FirstOrDefault()));
var notConnectedNodes = connectedNodes.Except(connectedNodes) .Each((i, index) => i.X = index * 100)
.Each(i => i.Y = 50);;
|
Linq 分页 dbconn.Records.OrderBy(p=>p.bid_id).Skip(skip).Take(take). skip= pageSize*(pageIndex-1),take=pageSize |
Linq的基本用用法的更多相关文章
- LinQ To Object 基本用法
http://www.cnblogs.com/terryzh/archive/2012/11/10/2763538.html LinQ To Object 基本用法 inq的基本语法:var resu ...
- Linq 中 Join 的用法
Linq中连接主要有组连接.内连接.左外连接.交叉连接四种.各个用法如下. 注:本文内容主要来自<Linq实战>,本例中用到的对象请见文章底部. 1. 组连接 组连接是与分组查询是一样的. ...
- linq lanbda表达式的用法
1. 查询Student表中的所有记录的Sname.Ssex和Class列.select sname,ssex,class from studentLinq: from s in Student ...
- LINQ中的Aggregate用法总结
Aggregate这个语法可以做一些复杂的聚合运算,例如累计求和,累计求乘积.它接受2个参数,一般第一个参数是称为累积数(默认情况下等于第一个值),而第二个代表了下一个值.第一次计算之后,计算的结果会 ...
- Linq学习以及简单用法
Linq学习 Linq(language Intergrated Query)即语言集成查询 LINQ是一组语言特性和API,使得你可以使用统一的方式编写各种查询.用于保存和检索来自不同数据源的数据, ...
- LINQ中Aggregate的用法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- linq中join的用法
join方法 public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>( this IEnu ...
- LinQ to SQL用法详解
LinQ是指集成化查询语言,通过映射将数据库内的表名变为C#的类名,将列名作为属性名,将表的关系作为类的成员对象.O--M--R O-Object对象(李昌辉)R-Relation关系M-Mappin ...
- System.Linq.Dynamic的使用
项目中经常用到组合条件查询,根据用户配置的查询条件进行搜索,拼接SQL容易造成SQL注入,普通的LINQ可以用表达式树来完成,但也比较麻烦.有个System.Linq.Dynamic用起来比较方便. ...
随机推荐
- intellij idea14 +svn配置
说明:使用TortoiseSVN客户端,安装时必须选择client tools,否则不会有svn.exe,也就不能支持intellij idea的svn插件,因为intellij idea是使用命令行 ...
- 个人总结-----非贪心算法的图的m着色判断及优化问题
1.问题描述: 对于著名的图的m着色,有两个主要的问题,一个是图的m色判定问题,一个是图的m色优化问题,描述如下. 图的m色判定问题: 给定无向连通图G和m种颜色.用这些颜色为图G的各顶点着色.问是否 ...
- 几个小模板:topology, dijkstra, spfa, floyd, kruskal, prim
1.topology: #include <fstream> #include <iostream> #include <algorithm> #include & ...
- ios 点击Home问题
应用可以在后台运行或者挂起,该场景的状态跃迁过程见图2-22,共经历3个阶段4个状态:Active → Inactive → Background→Suspended. q 在Active→Ina ...
- 安装Oracle客户端寻找配置文件tnsnames.ora
# tnsnames.ora Network Configuration File: D:\app\Administrator\product\11.2.0\dbhome_1\network\admi ...
- WEB服务器与应用服务器解疑
1.WEB服务器: 理解WEB服务器,首先你要理解什么是WEB?WEB你可以简单理解为你所看到的HTML页面就是WEB的数据元素,处理这些数据元素的应用软件就叫WEB服务器,如IIS.apache. ...
- php Pthread 多线程 (四) 共享内存
有些时候我们希望在多个线程中共享一些需要的数据,我们可以使用shmop扩展. <?php class Count extends Thread { private $name = ''; pub ...
- linux整合apache、php、mysql
1.打开apache配置文件,添加AddType.找到DirectoryIndex并添加index.php AddType application/x-httpd-php .php AddType a ...
- Castle ActiveRecord学习(三)数据映射及特性描述
Model中的Demo: using Castle.ActiveRecord; using Castle.ActiveRecord.Queries; using System; using Syste ...
- JTemplate学习(四)
注释.自定方法.模板嵌套子模板.循环输出不同class <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "htt ...