Linq查询案例
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查询案例的更多相关文章
- LINQ查询知识总结
-------适合自己的才是最好的!!! LINQ查询知识总结:案例分析 案例:汽车表car,系列表brand,厂商表productor private MyCarDataContext _Cont ...
- C#编程 LINQ查询
LINQ查询表达式 约束 LINQ查询表达式必须以from子句开头,以select或group子句结束 关键字 from...in...:指定要查找的数据以及范围变量,多个from子句则表示从多个数据 ...
- linq 查询的结果会开辟新的内存吗?
一:背景 1. 讲故事 昨天群里有位朋友问:linq 查询的结果会开辟新的内存吗?如果开了,那是对原序列集里面元素的深拷贝还是仅仅拷贝其引用? 其实这个问题我觉得问的挺好,很多初学 C# 的朋友或多或 ...
- Entity Framework 6 Recipes 2nd Edition(13-6)译 -> 自动编译的LINQ查询
问题 你想为多次用到的查询提高性能,而且你不想添加额外的编码或配置. 解决方案 假设你有如Figure 13-8 所示的模型 Figure 13-8. A model with an Associat ...
- LinqToDB 源码分析——轻谈Linq查询
LinqToDB框架最大的优势应该是实现了对Linq的支持.如果少了这一个功能相信他在使用上的快感会少了一个层次.本来笔者想要直接讲解LinqToDB框架是如何实现对Linq的支持.写到一半的时候却发 ...
- Linq查询基本操作
摘要:本文介绍Linq查询基本操作(查询关键字) - from 子句 - where 子句 - select子句 - group 子句 - into 子句 - orderby 子句 - join 子句 ...
- C#基础:LINQ 查询函数整理
1.LINQ 函数 1.1.查询结果过滤 :where() Enumerable.Where() 是LINQ 中使用最多的函数,大多数都要针对集合对象进行过滤,因此Where()在LINQ 的操作 ...
- 《Entity Framework 6 Recipes》中文翻译系列 (26) ------ 第五章 加载实体和导航属性之延缓加载关联实体和在别的LINQ查询操作中使用Include()方法
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 5-7 在别的LINQ查询操作中使用Include()方法 问题 你有一个LINQ ...
- Rafy 中的 Linq 查询支持(根据聚合子条件查询聚合父)
为了提高开发者的易用性,Rafy 领域实体框架在很早开始就已经支持使用 Linq 语法来查询实体了.但是只支持了一些简单的.常用的条件查询,支持的力度很有限.特别是遇到对聚合对象的查询时,就不能再使用 ...
随机推荐
- LIMIT语句解析及本章简单回顾(二十九)
一.LIMIT 限制查询结果返回的数量 [LIMIT {[offset,] row_count | row_count OFFSET offset}] select * from user; 除了可以 ...
- 关于iOS声音识别的框架
你好,我现在的项目中需要用到"声纹识别"这方面的需求,以前没做过,请教了.有没有这方面的框架和工具? 关于iOS声音识别的框架 >> ios这个答案描述的挺清楚的:ht ...
- [ Java ] [ Eclipse ] content Auto activation triggers
重點: That plug-in is not necessary any more. Just go to Preferences > Java > Editor > Conten ...
- PHP实现杨辉三角形
<?php /**** * 杨辉三角形:我的实现方式. * 下标 * 1 0 * 1 1 1 循环上一行数据1次,计算后结果追加到当前行末尾 * 1 2 1 2 * 1 3 3 1 3 * 1 ...
- 2017国家集训队作业[agc016b]Color Hats
2017国家集训队作业[agc016b]Color Hats 题意: 有\(N\)个人,每个人有一顶帽子.帽子有不同的颜色.现在,每个人都告诉你,他看到的所有其它人的帽子共有多少种颜色,问有没有符合所 ...
- 用树链剖分来写LCA
当两个点在一条链上,它们的LCA就是深度较小的那个点. 于是这种树链剖分写LCA的思想就是把要求的两个点想办法靠到一条链上. 而且要靠到尽量更优的一条链上(重链). 做法: 预处理出每棵树上的重链(s ...
- 二叉查找树BST 模板
二叉查找树BST 就是二叉搜索树 二叉排序树. 就是满足 左儿子<父节点<右儿子 的一颗树,插入和查询复杂度最好情况都是logN的,写起来很简单. 根据BST的性质可以很好的解决这些东 ...
- docker常用命令,学习笔记
- 常用命令 https://docs.docker.com images > docker images # 查看本地镜像 > docker images -a # 查看所(含中间镜像层 ...
- 人在IT,关于计算机专业的杂谈PPT
- div和css:行内元素和块元素的水平和垂直居中
行内元素: 水平居中:text-align:center ul水平居中:加 display:table; margin:0 auto; 此元素会作为块级表格来显示(类似 <table>), ...