LinQ—基本查询操作符 Select/Where/Group语句
来源:互联网 发布:写网页的软件 编辑:程序博客网 时间:2024/06/10 16:10
LinQ中的基本查询操作和SQL中的功能是一样的,对于其异同点,我们来了解了解:
1)Select
语法:
<span style="font-family:SimHei;font-size:18px;"> public static IEnumerable<TResult> Select<TSource,TResult>(this IEnumerable<TSource>source,Func<TSource,TResult>selector)</span>说明:
- Select方法本身是一个泛型集合扩展方法
- 它作用于IEnumerable<TSource>类型
- 它只接受一个Func<TSource,TResult>类型参数
- Func<TSource,TResult>是一个泛型委托,位于System名字空间下,System Core dll中,在这里selector是一个提取器。
2)Where
语法:
<span style="font-family:SimHei;font-size:18px;">public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource>source,Func<TSource,bool>predicate)</span>
说明:
- Where方法也是一个泛型扩展方法
- 它和Select()一样作用于IEnumerable<TSource>类型
- 它只接受一个Func<TSource,bool>泛型委托参数
- 在这里predicate是一个判断条件
3)Group 语法:
public static IEnumerable<IGrouping<TKey.TSource>>GroupBy<TSource.TKey>
(this IEnumerable<TSource>source,Func<TSource,TKey>keySelector)<span style="font-family: SimHei; background-color: rgb(255, 255, 255);"> </span>
4)举例:
首先我们来写一个扩展类,为IEnumerablet<string>提供输出的方法
<span style="font-family:SimHei;font-size:18px;"> //扩展类,只要是静态就可以 public static class ExtraClass { //为IEnumerablet<string>提供输出的方法 public static void Print(this IEnumerable<string> ie) { //获取可遍历的接口 IEnumerator<string> result = ie.GetEnumerator(); Console.WriteLine("\n-------------------------------------\n"); while (result.MoveNext()) { Console.WriteLine(result.Current); } Console.WriteLine("\n---------------------------------------\n"); } }</span>写一个泛型集合,为其存放几条数据:
<span style="font-family:SimHei;font-size:18px;"> private void BtnSelect_Click(object sender, EventArgs e) { //LinQ to Objects //泛型集合数据persons List<string> persons = new List<string>(); persons.Add("li si"); persons.Add("meng meng"); persons.Add("huo huo"); persons.Add("niu nan"); persons.Add("hu hu"); persons.Add("tu zi"); persons.Add("huo er"); persons.Add("huo xu");</span>这样我们就可以根据选择来显示输出结果:
1、输出person中所有元素:
<span style="font-family:SimHei;font-size:18px;"> //输出person中所有的元素 var result=persons.Select(p => p);</span>
显示结果:
2、输出person中姓huo的(以下三种语句都显示同样的效果)
<span style="font-family:SimHei;font-size:18px;"> //输出person中姓huo的(以下三种语句都必答同样的效果) //var result=persons.Where(p=>p.StartsWith("huo")); //var result = persons.Select(p=>p).Where(p => p.StartsWith("huo")); //var result = persons.Where(p => p.StartsWith("huo")).Select(p=>p);</span>显示的效果:
当然,也可以直接使用Where,利用bool委托参数来执行,
<span style="font-family:SimHei;font-size:18px;"> var result=persons.Where(p=>Judge(p)); result.Print(); } public bool Judge(string s) { if (s.StartsWith("huo")) { return true; } else { return false; } }</span>
显示效果一样,但是却直接显示了Where()语句的用法
对于Group的使用,我们可以使用其实现按照姓氏来显示名字如下:
//按照姓名来分组-取出姓名中的空格前的部分 var result=persons.GroupBy(p=>p.Split(new char[]{' '})[0]); foreach (var group in result) //循环姓氏显示出来 { Console.WriteLine("姓:" + group.Key); foreach (var name in group) //根据姓氏显示所对应的名字 { Console.WriteLine("\t" + name); //格式调整 } Console.WriteLine(); }
显示效果:
举得例子也只是一些常用的查询语句,其他的在继续学习中!
4 0
- LinQ—基本查询操作符 Select/Where/Group语句
- LINQ 的select 和where 查询语句总结
- LINQ标准查询操作符(一)——select、SelectMany、Where、OrderBy、OrderByDescending、ThenBy、ThenByDescending和Reverse
- LINQ标准查询操作符(一)——select、SelectMany、Where、OrderBy、OrderByDescending、ThenBy、ThenByDescending和Reverse
- LINQ查询操作符之Select、Where、OrderBy、OrderByDescending、GroupBy、Join、GroupJoin及其对应的查询语法
- oracle查询语句中select from where group by having order by的解释与应用
- oracle查询语句中select from where group by having order by的解释与应用
- oracle查询语句中select from where group by having order by的解释与应用
- 查询语句中select from where group by having order by的执行顺序
- 查询语句中select from where group by having order by的执行顺序
- 查询语句中select from where group by having order by的执行顺序
- 查询语句中select from where group by having order by的执行顺序
- 查询语句中select from where group by having order by的执行顺序
- 查询语句中select from where group by having order by的执行顺序
- 查询语句中select from where group by having order by的执行顺序
- 查询语句中select from where group by having order by的执行顺序
- 查询语句中select from where group by having order by的执行顺序
- SQL查询语句中select from where group by having order by的执行顺序
- YT14-HDU-#是@与.爱情的那堵墙
- pat1019 General Palindromic Number
- 基于颜色恒常性的低照度图像增强
- [NOTE] Windows&Linux动态链接库学习笔记
- BZOJ 2306 Ctsc2011 幸福路径 倍增Floyd
- LinQ—基本查询操作符 Select/Where/Group语句
- BZOJ大视野 2748: [HAOI2012]音量调节 解题报告
- 静态链接库的依赖顺序
- NoSql1 在Linux(CentOS)上安装memcached及使用
- A Byte of Python
- 雾和霾的区别
- Junit hamcrest使用
- 林达华08到10年的博客
- POJ1019 递