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 语法来查询实体了.但是只支持了一些简单的.常用的条件查询,支持的力度很有限.特别是遇到对聚合对象的查询时,就不能再使用 ...
随机推荐
- Car Talk1
This question is based on a Puzzler that was broadcast on the radioprogram Car Talk1: “I was driving ...
- Struts2+MySQL登录注册
下载地址:http://download.csdn.net/detail/qq_33599520/9777172 项目结构图: 代码: package com.mstf.action; import ...
- CentOS6.8下完全干净卸载mysql
来源整理于 https://www.cnblogs.com/wanghuaijun/p/6398240.html 虚拟机CentOS6.8下 先执行命令查看目录是否存在mysql 文件夹: cd ...
- T_SQL 日期函数
日期函数基数表达式的日期和时间或者是从时间间隔中返回值. GETDATE(),返回当前系统的日期和时间.例: SELECT GETDATE(); 结果为:2010-05-18 15:53:08.92 ...
- bzoj1618 购买干草
Description 约翰的干草库存已经告罄,他打算为奶牛们采购日(1≤日≤50000)磅干草.他知道N(1≤N≤100)个干草公司,现在用1到N给它们编号.第i个公司卖的干草包重量为Pi(1≤Pi ...
- easyui-combobox实现取值范围的联动
需求:需要用两个combobox来输入一个年月的范围,下拉框的内容从服务器获取.需要实现选中前者后,后者的下拉框中不能显示比前者数值小的:选中后者后,前者的下拉框内容不能显示比后者数值大的 有两个co ...
- Linux服务器性能评估与优化
一.影响务器性能因素 影响企业生产环境Linux服务器性能的因素有很多,一般分为两大类,分别为操作系统层级和应用程序级别.如下为各级别影响性能的具体项及性能评估的标准: (1)操作系统级别 内存: C ...
- BNUOJ 4049 四叉树
四叉树 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class name: Ma ...
- Redis中的持久化操作
本篇博客主要来解说一下怎样Redis中的持久化操作,当然了不是一篇理论性的博客,主要还是分享一下在redis中怎样来配置持久化操作. 1.介绍 redis为了内部数据的安全考虑,会把本身的数 ...
- HDU 2444 The Accomodation of Students 二分图判定+最大匹配
题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...