Linq案例
1.牛刀小试
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace linq
{
class Program
{
static void Main(string[] args)
{
string[] words = { "hello","linq","good","wonderful"};
var shortWords = from word in words
where word.Length <= 5
select word;
foreach (var word in shortWords)
{
Console.WriteLine(word);
}
Console.ReadKey();
}
}
}
2.Group分组处理
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace linq
{
class Program
{
static void Main(string[] args)
{
string[] words = { "hello","linq","good","wonderful","world","beautiful"};
//VAR 是3.5新出的一个定义变量的类型,其实也就是弱化类型的定义。VAR可代替任何类型,编译器会根据上下文来判断你到底是想用什么类型的。
var groups = from word in words
orderby word ascending
group word by word.Length into lengthGroups
orderby lengthGroups.Key descending
select new { Length = lengthGroups.Key, Words = lengthGroups }; // 按长度将单词分组
foreach (var group in groups)
{
Console.WriteLine("Words of length" + group.Length);
foreach (string word in group.Words)
{
Console.WriteLine(" " + word);
}
}
Console.ReadKey();
}
}
}
3.XML 案例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Threading.Tasks;
namespace linq
{
class Book
{
public string Publisher;
public string Title;
public int Year;
public Book(string title,string publisher,int year)
{
Title = title;
Publisher = publisher;
Year = year;
}
}
class Program
{
static void Main(string[] args)
{
Book[] books = new Book[]
{
new Book("Ajax","Zhang",2015),
new Book("Java","Li",2016),
new Book(".Net","Zhao",2016),
};
XElement xml = new XElement("books",
from book in books
where book.Year == 2016
select new XElement("book",
new XAttribute("title",book.Title),
new XElement("publisher",book.Publisher)
)
);
Console.WriteLine(xml);
Console.ReadKey();
}
}
}
Linq案例的更多相关文章
- [原创]Linq to xml增删改查Linq 入门篇:分分钟带你遨游Linq to xml的世界
本文原始作者博客 http://www.cnblogs.com/toutou Linq 入门篇(一):分分钟带你遨游linq to xml的世界 本文原创来自博客园 请叫我头头哥的博客, 请尊重版权, ...
- linq的语法和案例
本篇逐一介绍linq各个关键字的用法(from,select,group,into等),本篇所有的案例都是用linqpad来完成的(官方地址:http://www.linqpad.net/),建议想学 ...
- LINQ简单案例
1.在visual studio 创建一个解决方案,新建一个控制台程序Kong 2.新建两个类,分别为Master 类和Kongfu类 Master类中包含成员如下,并重写ToString方法 na ...
- linq中order by 和group by (含lambda表达式实现)以及综合案例
一.Linq应用场景 linq的语法通过System.Linq下面的Enumerable类提供支持,也就是说,只要是实现了IEnumerable<T>的对象都可以使用Linq的语法来查询. ...
- Linq系列(5)——表达式树之案例应用
在进入今天的正题之前,先感慨下本人的blog的人气一篇不如一篇.再加上换公司后人身自由受到了比之前大得多得多的限制,实在令本人有些郁闷.不过每次提笔写些东西跟大家分享,总是能让我感到愉悦和欣慰,希望我 ...
- Linq查询案例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- linq详细案例
LINQ to SQL语句(1)之Where 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句.Where操 ...
- Microsoft Visual Studio 2017 for Mac Preview 下载+安装+案例Demo
目录: 0. 前言 1. 在线安装器 2. 安装VS 3. HelloWorld 4. ASP.NET MVC 5. 软件下载 6. 结尾 0. 前言: 工作原因,上下班背着我的雷神,一个月瘦了10斤 ...
- Redis简单案例(一) 网站搜索的热搜词
对于一个网站来说,无论是商城网站还是门户网站,搜索框都是有一个比较重要的地位,它的存在可以说是 为了让用户更快.更方便的去找到自己想要的东西.对于经常逛这个网站的用户,当然也会想知道在这里比较“火” ...
随机推荐
- ZoomIt(投影演示辅助软件)下载、安装与运行使用
下载ZoomIt后,打开即可使用:打开时,你讲看到如下的几个页面,这几个页面是为了介绍每个功能的使用,还可以去设定你觉得比较舒服的快捷键, 默认的是Ctrl+1屏幕放大.Ctrl+2屏幕标注,Ctrl ...
- GridView 绑定 ObjectDataSource
创建GridView <asp:GridView ID="GridView1" runat="server" DataSourceID="Obj ...
- CUDA笔记(六)
dim3是NVIDIA的CUDA编程中一种自定义的整型向量类型,基于用于指定维度的uint3 忽然发现需要再搞多机MPI的配置,多机GPU集群.好麻烦.. 这两天考完两门了,还剩下三门,并行计算太多了 ...
- Xor Sum 2(位运算)
D - Xor Sum 2 Time limit : 2sec / Memory limit : 1024MB Score : 500 points Problem Statement There i ...
- DedeCMS文章编辑不更新时间1970年1月1日
在修改文章或者后期优化的时候,织梦dedecms5.7版本存在一个问题,修改文章的同时也修改了文章的发布时间,这个功能可能有些人比较需要,但同时也有些站长朋友又不需要,因为我们编辑某个文章的时候,发现 ...
- ArchLinux dwm的安装和配置
dwm官网:https://dwm.suckless.org/ dwm是一个简洁的平铺式窗口管理器 配置简单,使用便捷,没有多少依赖,占用内存非常小 总之dwm正合口味 安装方法 首先在官网下载dwm ...
- 前端之CSS选择器
基本选择器 元素选择器 p {color: "red";} ID选择器 #i1 { background-color: red; } 类选择器 .c1 { font-size: 1 ...
- Systemd曝3漏洞,大部分Linux将受到攻击
Linux 系统与服务管理工具 Systemd 被曝存在 3 大漏洞,影响几乎所有 Linux 发行版. Systemd 是 Linux 系统的基本构建块,它提供了对系统和服务的管理功能,以 PID ...
- 编码问题异常处理:UnicodeDecodeError: 'gbk' codec can't...
作为编码问题集合: 2)UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbd in position 0: invalid start by ...
- 洛谷—— P2419 [USACO08JAN]牛大赛Cow Contest
https://www.luogu.org/problem/show?pid=2419 题目背景 [Usaco2008 Jan] 题目描述 N (1 ≤ N ≤ 100) cows, convenie ...