using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinqDemo
{
class Program
{
static void Main(string[] args)
{
string[] names = { "Alonso","Zheng","Small","Smile"};
var queryResults = from n in names where n.StartsWith("S") select n; // n代表某一个元素,where指定查询的条件,select指定包含的元素
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
} Console.ReadLine();
}
}
}

改造后,功能一样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinqDemo
{
class Program
{
static void Main(string[] args)
{
string[] names = { "Alonso","Zheng","Small","Smile","Ruiz","Singh"};
// var queryResults = from n in names where n.StartsWith("S") select n; // n代表某一个元素,where指定查询的条件,select指定包含的元素
var queryResults = names.Where(n => n.StartsWith("S"));
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
} Console.ReadLine();
}
}
}

增加排序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinqDemo
{
class Program
{
static void Main(string[] args)
{
string[] names = { "Alonso","Zheng","Small","Smile","Ruiz","Singh"};
var queryResults = from n
in names
where n.StartsWith("S")
orderby n
select n; // n代表某一个元素,where指定查询的条件,select指定包含的元素
// var queryResults = names.Where(n => n.StartsWith("S"));
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
} Console.ReadLine();
}
}
}

降序排列

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinqDemo
{
class Program
{
static void Main(string[] args)
{
string[] names = { "Alonso","Zheng","Small","Smile","Ruiz","Singh"};
var queryResults = from n
in names
where n.StartsWith("S")
orderby n descending // Z-A将序
select n; // n代表某一个元素,where指定查询的条件,select指定包含的元素
// var queryResults = names.Where(n => n.StartsWith("S"));
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
} Console.ReadLine();
}
}
}

按照最后一个字母排序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinqDemo
{
class Program
{
static void Main(string[] args)
{
string[] names = { "Alonso","Zheng","Small","Smile","Ruiz","Singh"};
var queryResults = from n
in names
where n.StartsWith("S")
orderby n.Substring(n.Length -1) // 按最后一个字母排序
select n; // n代表某一个元素,where指定查询的条件,select指定包含的元素
// var queryResults = names.Where(n => n.StartsWith("S"));
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
} Console.ReadLine();
}
}
}

通过OrderBy方法排序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinqDemo
{
class Program
{
static void Main(string[] args)
{
string[] names = { "Alonso","Zheng","Small","Smile","Ruiz","Singh"};
//var queryResults = from n
// in names
// where n.StartsWith("S")
// orderby n.Substring(n.Length -1) // 按最后一个字母排序
// select n; // n代表某一个元素,where指定查询的条件,select指定包含的元素
var queryResults = names.OrderBy(n => n).Where(n => n.StartsWith("S")); // 通过OrderBy方法排序
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
} Console.ReadLine();
}
}
}

大数据查询

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LinqDemo
{
class Program
{
static void Main(string[] args)
{
int[] numbers = GenerateLotsOfNumbers(123456789);
var queryResults = from n in numbers
where n < 1000
select n;
Console.WriteLine("小于1000的数字:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
} Console.ReadLine();
} // 随机数列表
private static int[] GenerateLotsOfNumbers(int count)
{
Random generator = new Random(0);
int[] result = new int[count];
for (int i = 0; i< count; i++)
{
result[i] = generator.Next();
}
return result;
}
}
}

Linq查询案例的更多相关文章

  1. LINQ查询知识总结

    -------适合自己的才是最好的!!! LINQ查询知识总结:案例分析 案例:汽车表car,系列表brand,厂商表productor private MyCarDataContext  _Cont ...

  2. C#编程 LINQ查询

    LINQ查询表达式 约束 LINQ查询表达式必须以from子句开头,以select或group子句结束 关键字 from...in...:指定要查找的数据以及范围变量,多个from子句则表示从多个数据 ...

  3. linq 查询的结果会开辟新的内存吗?

    一:背景 1. 讲故事 昨天群里有位朋友问:linq 查询的结果会开辟新的内存吗?如果开了,那是对原序列集里面元素的深拷贝还是仅仅拷贝其引用? 其实这个问题我觉得问的挺好,很多初学 C# 的朋友或多或 ...

  4. Entity Framework 6 Recipes 2nd Edition(13-6)译 -> 自动编译的LINQ查询

    问题 你想为多次用到的查询提高性能,而且你不想添加额外的编码或配置. 解决方案 假设你有如Figure 13-8 所示的模型 Figure 13-8. A model with an Associat ...

  5. LinqToDB 源码分析——轻谈Linq查询

    LinqToDB框架最大的优势应该是实现了对Linq的支持.如果少了这一个功能相信他在使用上的快感会少了一个层次.本来笔者想要直接讲解LinqToDB框架是如何实现对Linq的支持.写到一半的时候却发 ...

  6. Linq查询基本操作

    摘要:本文介绍Linq查询基本操作(查询关键字) - from 子句 - where 子句 - select子句 - group 子句 - into 子句 - orderby 子句 - join 子句 ...

  7. C#基础:LINQ 查询函数整理

    1.LINQ 函数   1.1.查询结果过滤 :where() Enumerable.Where() 是LINQ 中使用最多的函数,大多数都要针对集合对象进行过滤,因此Where()在LINQ 的操作 ...

  8. 《Entity Framework 6 Recipes》中文翻译系列 (26) ------ 第五章 加载实体和导航属性之延缓加载关联实体和在别的LINQ查询操作中使用Include()方法

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 5-7  在别的LINQ查询操作中使用Include()方法 问题 你有一个LINQ ...

  9. Rafy 中的 Linq 查询支持(根据聚合子条件查询聚合父)

    为了提高开发者的易用性,Rafy 领域实体框架在很早开始就已经支持使用 Linq 语法来查询实体了.但是只支持了一些简单的.常用的条件查询,支持的力度很有限.特别是遇到对聚合对象的查询时,就不能再使用 ...

随机推荐

  1. The python programing language

    Python is an example of high-level language. As you might infer from the name “high-level language”, ...

  2. go语言中在变量后加上接口是什么意思?

    如题刚刚开始学习go 语言有些不懂: a.Data = make(map[string]interface{}) 我认为它是在申请a.Data map为字符串类型的空间,那么它后面接一个空的inter ...

  3. PostgreSQL Replication之第七章 理解Linux高可用(2)

    7.2 衡量可用性 可用性是提供商试图保证一定的可用性级别和客户可以期望的可用性或更多.在某些情况下(取决于服务合同) 收取罚款或减少申购费用是意外停机的原因. 可用性的质量使用百分数来衡量:例如,9 ...

  4. js字符串首字母转为大写

    function initialsLetterUpperCase(arr){ if(Array.isArray(arr)){ return arr.map(function(val,index,arr ...

  5. Design Doc: Session History for Out-of-Process iframes

    Design Doc: Session History for Out-of-Process iframes Charlie Reis, May 2014 This document outlines ...

  6. Linux Putty 复制粘贴

    从putty复制:    用左键选中文字,再其他地方点中键就可以粘贴了,不需要右键粘贴. 在putty中粘贴:    在其他地方用左键选中文字,不用右键复制,保持选中文字高亮,然后在putty中点中键 ...

  7. type与isinstance使用区别

    Python中,type与isinstance都可以用来判断变量的类型,但是type具有一定的适用性,用它来判断变量并不总是能够获取到正确的值. Python在定义变量的时候不用指明具体的的类型,解释 ...

  8. Django初学习程序大致流程

  9. springMVC的rest风格的url请求

    rest是一个架构风格,用url来访问网络上的任何资源.rest的一种思想就是用http中的动作get,post,put,delete,来进行增删改查. 这里介绍的是springMVC的rest请求. ...

  10. 零基础学python-3.3 标识符

    1.标识符的组成 1)有数字.下划线.英文字母组成 2)第一个字符仅仅能是字母或者下划线 3)大写和小写敏感 标识符通常是变量名称.方法名.类名等 2.keyword python里面有一系列的关键字 ...