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. hdu1978 How many ways

    How many ways Problem Description 这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m).游戏的规则描述如下: 机器人一开始在棋 ...

  2. 实时监控Cat之旅~对Get和Post进行封装,支持分布式消息树

    对第三方接口的调用我们需要对GET和POST进行监控,看一些请求的执行是否成功,如A调用B,B调用C,C调用D,这一连串的东西需要我们使用cat进行记录,进行记录之后,我们可以很容易的发现请求响应的时 ...

  3. java9新特性-6-多版本兼容jar包

    1.官方Feature 238: Multi-Release JAR Files 2.使用说明 当一个新版本的Java出现的时候,你的库用户要花费数年时间才会切换到这个新的版本.这就意味着库得去向后兼 ...

  4. 关于 nginx 的配置的 location

    精准匹配和普通匹配:    server{            location =/index.htm{                                       ////精准匹 ...

  5. 学习Go语言之使用原子访问或互斥锁解决竞态问题

    使用原子访问或互斥锁 // 解决竞态问题 package main import ( "fmt" "sync" "sync/atomic" ...

  6. 在angular4.X里使用mCustomScrollbar滚动条插件

    参考网上的方法https://stackoverflow.com/questions/36755625/how-to-import-jquery-and-mcustomscrollbar-plugin ...

  7. HDU-1878 欧拉回路 欧拉回路

    题目链接:https://cn.vjudge.net/problem/HDU-1878 题意 中文题,而且就是单纯的欧拉回路 思路 判断连通图 用并查集会很好,bfs亦可 一时脑抽用bfs过了这个题, ...

  8. HDU-4310 Hero 贪心问题

    题目链接:https://cn.vjudge.net/problem/HDU-4310 题意 打dota,队友太菜,局势变成1vN.还好你开了挂,hp无限大(攻击却只有一点每秒-_-). 但是你并不想 ...

  9. HDU-2045 不容易系列之(3)—— LELE的RPG难题 找规律&递推

    题目链接:https://cn.vjudge.net/problem/HDU-2045 找规律 代码 #include <cstdio> long long num[51][2]; int ...

  10. [POI2005]DWU-Double-row(图论?)

    题意 2n 个数站成两排(每个数在 2n个数中最多出现两遍),一次操作可以交换任意一列中两个数,求使每行数不重复的最少操作数. (n<=50000) 题解 说实话,我真没想到图论.(我太菜了) ...