Linq To Object
//SelectMany
List<List<int>> Numbers = new List<List<int>>()
{
new List<int>{1,2,3},
new List<int>{4,5,6},
new List<int>{7,8,9} }; var result = Numbers.SelectMany(a => a);
foreach (var nubers in result)
{
Console.WriteLine(nubers);
} int[][] intarry = { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 }, new int[] { 7, 8, 9 } };
var result = intarry.SelectMany(a => a);
foreach (var nubers in result)
{
Console.WriteLine(nubers);
}
//Take:返回指定的元素个数=>从左至右
string[] arry = new string[] { "1", "2", "3", "4", "5" };
var result = (from query in arry select query).Take(2);
//var result= arry.Take(2);
foreach (var it in result)
{
Console.WriteLine(it);
}
//TakeWhile:返回连续满足条件的元素=>从左至右
string[] arry = new string[] { "1234", "2345", "345", "456", "567" };
var item = (from query in arry select query).TakeWhile(q => q.Length>3);
//var item = arry.TakeWhile(q => q.Length==4);
foreach (var it in item)
{
Console.WriteLine(it);
}
//Skip:跳过指定的元素个数=>从左至右
string[] ary = new string[] { "asp.net", "csharp","22", "xhtml", "css", "javascript", "wcf", "wpf",
"asp.net", "csharp", "xhtml", "css", "javascript","silverlight"};
var result = (from query in ary select query).Skip(8);
//var result = ary.Skip(8);
foreach (var it in result)
{
Console.WriteLine(it);
}
//SkipWhile:跳过连续满足条件的元素
string[] ary = new string[] { "asp.net", "csharp","22", "xhtml", "css", "javascript", "wcf", "wpf",
"asp.net", "csharp", "xhtml", "css", "javascript","silverlight"};
var result = ary.SkipWhile(q => q.Length > 3).TakeWhile(q => q.Length >= 2).Take(5);
foreach (var it in result)
{
Console.WriteLine(it);
}
//Concat:连接不同集合,不会自动过滤相同项
string[] ary = new string[] { "asp.net", "csharp","22", "xhtml", "css", "javascript", "wcf", "wpf",
"asp.net", "csharp", "xhtml", "css", "javascript","silverlight",
"linq", "wf", "sqlserver", "asp.net ajax", "ssis", "ssas", "ssrs"
};
string[] arry = new string[] { "1234", "2345", "345", "456", "567" };
var result = ary.Concat(arry);
foreach (var it in result)
{
Console.WriteLine(it);
}
//OrderBy:正序排列
string[] ary = new string[] { "asp.net", "csharp","22", "xhtml", "css", "javascript", "wcf", "wpf",
"asp.net", "csharp", "xhtml", "css", "javascript","silverlight" };
var result = ary.OrderBy(q => q.Length);
foreach (var it in result)
{
Console.WriteLine(it);
}
//OrderByDescending:倒序排列
string[] ary = new string[] { "asp.net", "csharp","22", "xhtml", "css", "javascript", "wcf", "wpf",
"asp.net", "csharp", "xhtml", "css", "javascript","silverlight",
"linq", "wf", "sqlserver", "asp.net ajax", "ssis", "ssas", "ssrs"
};
var result = ary.OrderByDescending(q => q.Length);
foreach (var it in result)
{
Console.WriteLine(it);
}
//ThenBy:对OrderBy与OrderByDescending排序的结果再进行以特定的条件进行正序排序
string[] ary = new string[] { "asp.net", "csharp","22", "xhtml", "css", "javascript", "wcf", "wpf",
"asp.net", "csharp", "xhtml", "css", "javascript","silverlight" };
var result = ary.OrderByDescending(q=>q.Length).ThenBy(q =>q);
foreach (var it in result)
{
Console.WriteLine(it);
}
//Distinct:过滤集合中的相同项;
string[] ary = new string[] { "asp.net", "csharp","22", "xhtml", "css", "javascript", "wcf", "wpf",
"asp.net", "csharp", "xhtml", "css", "javascript","silverlight",
"linq", "wf", "sqlserver", "asp.net ajax", "ssis", "ssas", "ssrs"
};
var result = ary.Distinct();
foreach (var it in result)
{
Console.WriteLine(it);
}
//Union:连接不同集合,自动过滤相同项;
string[] ary = new string[] { "asp.net", "csharp","22","456", "xhtml", "css", "javascript", "wcf", "wpf",
"asp.net", "csharp", "xhtml", "css", "javascript","silverlight",
"linq", "wf", "sqlserver", "asp.net ajax", "ssis", "ssas", "ssrs"
};
string[] arry = new string[] { "1234", "2345", "345", "456", "567", "asp.net", "asp.net ajax", };
var result = ary.Union(arry);
foreach (var it in result)
{
Console.WriteLine(it);
}
//Intersect:获取不同集合的相同项(交集)
string[] ary = new string[] { "asp.net", "csharp","22","456", "xhtml", "css", "javascript", "wcf", "wpf",
"asp.net", "csharp", "xhtml", "css", "javascript","silverlight",
"linq", "wf", "sqlserver", "asp.net ajax", "ssis", "ssas", "ssrs"
};
string[] arry = new string[] { "1234", "2345", "345", "456", "567", "asp.net", "asp.net ajax", };
var result = ary.Intersect(arry);
foreach (var it in result)
{
Console.WriteLine(it);
}
//Except:从某集合中删除其与另一个集合中相同的项,并消除自身的重复项
string[] ary = new string[] { "asp.net", "csharp","22","456", "xhtml", "css", "javascript", "wcf", "wpf",
"asp.net", "csharp", "xhtml", "css", "javascript","silverlight",
"linq", "wf", "sqlserver", "asp.net ajax", "ssis", "ssas", "ssrs"
};
string[] arry = new string[] { "1234", "2345", "345", "456", "567", "asp.net", "asp.net ajax", };
var result = ary.Except(arry);
foreach (var it in result)
{
Console.WriteLine(it);
}
//Single:根据表达式返回集合中的某一元素
string[] ary = { "css", "javascript", "sqlserver"};
//var result = ary.Single(q => q == "sqlserver" || q[2] == 'l');
var result = (from query in ary select query).Single(q => q == "sqlserver" || q[2] == 'l');
foreach (var it in result)
{
Console.Write(it);
}
//SingleOrDefault:根据表达式返回集合中的某一元素(如果没有则返回默认值)
string[] ary = new string[] {"22","456", "wcf", "wpf",
"asp.net", "csharp", "xhtml", "css", "javascript","silverlight",
"linq", "wf", "sqlserver", "asp.net ajax", "ssis", "ssas", "ssrs"
};
var result="";
result = ary.SingleOrDefault(q => q == "javascript"); foreach (var it in result)
{
Console.Write(it);
}
//Reverse:对集合反向排序
string[] ary = new string[] {"22","456", "wcf", "wpf","wpf","wpf","wpf","wpf",
"asp.net", "csharp", "xhtml", "css", "javascript","silverlight",
"linq", "wf", "sqlserver", "asp.net ajax", "ssis", "ssas", "ssrs"
};
var result = ary.Reverse(); foreach (var it in result)
{
Console.WriteLine(it);
}
//Join:用来连接两个输入序列
string[] ary = new string[] {"22","456", "wcf", "wpf",
"asp.net", "csharp", "xhtml", "css", "javascript","silverlight",
"linq", "wf", "sqlserver", "asp.net ajax", "ssis", "ssas", "ssrs"
};
string[] arry = new string[] { "456", "asp.net", "asp.net ajax" };
var result = from _query in ary
join query in arry on _query equals query
select _query; foreach (var it in result)
{
Console.WriteLine(it);
}
//Cast:Cast操作符将类型为IEnumerable的集合对象转换成为IEnumerable<T>类型的集合对象
ArrayList arraylist = new ArrayList();
arraylist.Add("1111");
arraylist.Add("222");
arraylist.Add("3333");
IEnumerable<string> IE = arraylist.Cast<string>().Where(A => A.Length > 3);
foreach (string item in IE)
{
Console.WriteLine(item);
}
//结果:1111 3333
//ToArray:将一个输入序列转换成一个数组
List<string> list = new List<string>();
list.AddRange(new List<string>
{
"1","2","3","4","5"
});
string[] str = list.ToArray();
//ToList:操作符用于将一个输入序列转换成一个List<T>(System.Collections.Generic.List<T>)对象。
string[] arry = new string[] { "456", "asp.net", "asp.net ajax" };
List<string> str = arry.ToList();
//ToDictionary:操作符用于将一个输入序列转换成一个Dictionary<K,T>(System.Collections.Generic. Dictionary<K,T>)集合对象
List<Class> cla=new List<Class>();
for (int i = 0; i < 10; i++)
{
cla.Add(new Class { ClassID = i, ClassName = i.ToString(), StudentNumber = i + 30 });
} Dictionary<int, string> dic = cla.ToDictionary(new Func<Class, int>(A => A.ClassID), new Func<Class, string>(B => B.ClassName));
foreach (var item in dic.Keys)
{
Response.Write(item+"<br/>");
}
//ToLookup:操作符可以将一个输入序列转换成一个ILookup<K,T> (System.Linq.ILookup<K,T>)集合对象。ILookup<K,T>集合对象与Dictionary<K,T>集合对象非常相似,不同的是,在Dictionary<K,T>中一个键(key)只能对应一个元素;而在ILookup<K,T>中,一个键可以对应一组元素(一个序列)。
List<Class> cla = new List<Class>();
for (int i = 0; i < 10; i++)
{
cla.Add(new Class { ClassID = i, ClassName = i.ToString(), StudentNumber = i + 30 });
}
ILookup<int,string> Lookup= cla.ToLookup(new Func<Class, int>(A => A.ClassID), new Func<Class, string>(B => B.ClassName));
foreach (var item in Lookup)
{
Response.Write(item.Key + "<br/>");
}
//SequenceEqual:判断两个序列是否相等
int[] arr = { 4, 5, 6 };
int[] array = { 4, 5, 6 };
bool b = arr.SequenceEqual(array);
Console.WriteLine(b);
//First:返回输入序列的第一个元素或者满足条件的第一个元素
string[] arr = { "4", "52", "666666666" };
string str = arr.First(A => A.Length > 5);
Console.WriteLine(str);
//结果:666666666
//FirstOrDefault:操作符的使用与First操作符非常相似,只是在没有找到元素时,First操作符会抛出一个异常对象,而FirstOrDefault操作符会返回一个相关类型默认值元素
string[] arr = { "4", "52", "66666" };
string array= arr.FirstOrDefault(new Func<string, bool>(A => A.Length>5));
Console.WriteLine(array);
//Last操作符用于返回输入序列的最后一个元素或者满足条件的最后一个元素
string[] arr = { "4", "52", "666666666" };
string str = arr.Last(A => A.Length > 5);
Console.WriteLine(str);
//LastOrDefault操作符的使用与Last操作符非常相似,只是在没有找到元素时,Last操作符会抛出一个异常对象,而LastOrDefault操作符会返回一个相关类型默认值元素 string[] arr = { "4", "52", "6666666" };
string array = arr.LastOrDefault(new Func<string, bool>(A => A.Length > 5));
Console.WriteLine(array);
//Single操作符用于从一个只含有一个元素的输入序列中返回唯一的元素或者满足特定条件的唯一元素。如果输入序列中的元素个数为零或者多于一个,Single操作符会抛出一个异常
string[] arr = { "4", "52", "6666666" };
string array = arr.Single(new Func<string, bool>(A => A.Length > 5));
Console.WriteLine(array);
//SingleOrDefault操作符的使用与Single操作符非常相似,只是在没有找到元素时,Single操作符会抛出一个异常对象,而SingleOrDefault操作符会返回一个相关类型默认值元素
string[] arr = { "4", "52", "6666655" };
string array = arr.SingleOrDefault(new Func<string, bool>(A => A.Length > 5));
Console.WriteLine(array);
//ElementAt操作符用于返回输入序列中指定位置的元素
string[] arr = { "4", "52", "6666655" };
string array = arr.ElementAt(2);
Console.WriteLine(array);
//ElementAtOrDefault操作符的使用与ElementAt操作符非常相似,只是在指定的位置索引值不合法的情况下(小于0或者大于或等于序列元素个数),ElementAt操作符会抛出一个异常对象,而ElementAtOrDefault操作符会返回一个相关类型默认值元素
string[] arr = { "4", "52", "6666655" };
string array = arr.ElementAtOrDefault(3);
Console.WriteLine(array);
//Any:操作符用于判断一个输入序列中是否含有元素或者含有满足特定条件的元素
string[] arr = { "4", "52", "6666655" };
bool b = arr.Any(new Func<string, bool>(A => A.Length == 8));
Console.WriteLine(b); //结果:False
//All操作符用于判断输入序列中的所有元素是否全部满足指定条件
string[] arr = { "4", "52", "6666655" };
bool b= arr.All(new Func<string, bool>(A => A.GetType() == typeof(string)));
Console.WriteLine(b); //结果:True
//Count:操作符用于获取输入序列的元素个数或者满足某个特定条件的元素个数
string[] arr = { "4", "52", "6666655" };
int I = arr.Count(new Func<string, bool>(A => A.Length > 2));
Console.WriteLine(I); //结果:1
//Sum:计算输入序列中所有元素数值总和
int[] arr = { 4, 52, 66};
int SUN = arr.Sum(new Func<int, int>(A => A));
Console.WriteLine(SUN); //结果:122
DataSet ds = Common.SqlDataAdapter(ConnStr.connStr, "select * from View_Salary", null);
var select = from query in ds.Tables[0].AsEnumerable() where query.Field<int>("Salary") > 0 select query;
int SumSalary = select.Sum(query => query.Field<int>("Salary"));
return SumSalary;
//Min:获取输入序列中所有元素的最小值
int[] arr = { 4, 52, 66};
int SUN = arr.Min(new Func<int, int>(A => A));
Console.WriteLine(SUN); //结果:4
//Max:操作符用于获取输入序列中所有元素的最大值
int[] arr = { 4, 52, 66};
int SUN = arr.Max(new Func<int, int>(A => A));
Console.WriteLine(SUN); //结果:66
//Average:操作符用于计算输入序列中全体元素的平均值
int[] arr = { 4, 52, 66};
double SUN = arr.Average(new Func<int, int>(A => A));
Console.WriteLine(SUN); //结果:40.6666666666667
//Select
var persons = new List<Person> {
new Person{userName="action", age=11},
new Person{userName="Boolean", age=26},
new Person{userName="new",age=25},
};
//var v = from fm in persons where fm.age > 20 select fm.userName.ToUpper();
var v = persons.Where(fm => fm.age > 20).Select(fm => fm.userName.ToUpper());
foreach (var item in v)
{
Console.WriteLine(item.ToString());
}
//AsEnumerable与orderby...ascending,正序
DataSet ds = Common.SqlDataAdapter(ConnStr.connStr, "select * from View_Salary", null);
var select = from query in ds.Tables[0].AsEnumerable() orderby query.Field<int>("Salary") ascending select query;
DataTable MyDataTable = select.CopyToDataTable<DataRow>();
return MyDataTable;
//orderby...descending,倒序
DataSet ds = Common.SqlDataAdapter(ConnStr.connStr, "select * from View_Salary", null);
var select = from query in ds.Tables[0].AsEnumerable() orderby query.Field<int>("Salary") descending select query;
DataTable MyDataTable = select.CopyToDataTable<DataRow>();
return MyDataTable;
Linq To Object的更多相关文章
- .NET面试题系列[13] - LINQ to Object
.NET面试题系列目录 名言警句 "C# 3.0所有特性的提出都是更好地为LINQ服务的" - Learning Hard LINQ是Language Integrated Que ...
- LINQ系列:Linq to Object投影操作符
投影是指在将序列中的元素转换为一个自定义形式的操作.投影操作符Select和SelectMany用于选择出赋予了适当功能的值.SelectMany操作符可以处理多个集合. LINQ表达式语法: 1. ...
- LINQ系列:Linq to Object生成操作符
生成操作符从现有序列值中创建新的序列. 1. Empty Empty操作符返回一个指定类型的空集. 1>. 原型定义 public static IEnumerable<TResult& ...
- LINQ系列:Linq to Object转换操作符
转换是指将输入对象的类型转变为序列的动作. 1. AsEnumerable AsEnumerable操作符将查询的输入以IEnumberable(T)类型返回. 2. Cast Cast操作符将IEn ...
- LINQ系列:Linq to Object量词操作符
量词操作符返回一个Boolean值,指示序列中是否存在部分或全部元素符号指定条件.LINQ中量词操作符包括:All.Any和Contains. 1. All All操作符判定在集合中是否所有的值都满足 ...
- LinQ To Object 基本用法
http://www.cnblogs.com/terryzh/archive/2012/11/10/2763538.html LinQ To Object 基本用法 inq的基本语法:var resu ...
- SQO (标准查询运算符)方法 & Linq To Object
#region SQO (标准查询运算符) 方法 #region Where() Find() FindAll() FirstOrDefault()等方法 static void c01where() ...
- 1.解剖Linq to object
LINQ想必大家都不陌生了,它的出现使得我们的代码变得更短.更优雅了.至于LINQ是什么,Linq to object这类的扩展方法到底做了些什么.我们使用的EF是如何实现的(如何解析Expressi ...
- Linq to EF 与Linq to Object 使用心得
大家都知道Linq既可以用来查询数据库对象(我这里指的是Entity FrameWork里的Model对象),也可以用来查询内存中的IEnumerable对象. 两者单独查询时都不会出现什么问题,不过 ...
- Linq to OBJECT延时标准查询操作符
1.Where 操作符用于限定输入集合中的元素,将符合条件的元素组织声称一个序列结果.2.Select 操作符用于根据输入序列中的元素创建相应的输出序列中的元素,输出序列中的元素类型可以与输入序列中 ...
随机推荐
- UVa10806 Dijkstra,Dijkstra-费用网络流
Problem, in short Given a weighed, undirected graph, find the shortest path from S to T and back wit ...
- VMware Workstation(虚拟机)v10.0.1 简体中文破解版
http://www.xp510.com/xiazai/ossoft/desktools/22610.html
- Android 实现多行文本跑马灯效果
Android TextView 实现跑马灯的效果很简单,只要加三个属性就可以了. android:ellipsize="marquee" android:focusable=&q ...
- Python自动化 【第六篇】:Python基础-面向对象
目录: 面向过程VS面向对象 面向对象编程介绍 为什么要用面向对象进行开发 面向对象的特性:封装.继承.多态 面向过程 VS 面向对象 面向过程编程(Procedural Programming) ...
- HP XP7 GAD双活实现的理解
XP7双活的虚拟卷global active device (GAD)实际上对应两个存储的两个物理卷(有点儿像Mirror Disk镜像) 当主机A向阵列A发出写数据请求后,阵列A首先检查要被写入的数 ...
- PHP 用html方式输出Excel文件时的数据格式设置
1) 文本:vnd.ms-excel.numberformat:@ 2) 日期:vnd.ms-excel.numberformat:yyyy/mm/dd 3) 数字:vnd.ms-excel.numb ...
- [华清远见]FPGA公益培训
本套视频教程为华清远见 网络公益培训活动,主讲人:姚远老师,华清远见高级讲师. ------------------------------------------------------------ ...
- ORACLE 10进制与16进制的互相转换
1. 10---->16 使用to_char(10,'xxx')函数,如果位数长,多加几个 x 2. 16---->10 使用to_number(’a','xxx')函数,如果位数长,多加 ...
- search--搜索引擎的使用笔记
重度使用 完全匹配搜索 “”把搜索词放在双引号中 搜索不包含该词 减号- 常用的通配符 星号* 站内搜索site docker site:http://blog.daocloud.io/ 扩大范围搜索 ...
- access数据库连接问题
使用Visual Studio连接access数据库(数据库后缀.accdb)时连接不上(access数据库提示未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序)解决办 ...