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案例的更多相关文章

  1. [原创]Linq to xml增删改查Linq 入门篇:分分钟带你遨游Linq to xml的世界

    本文原始作者博客 http://www.cnblogs.com/toutou Linq 入门篇(一):分分钟带你遨游linq to xml的世界 本文原创来自博客园 请叫我头头哥的博客, 请尊重版权, ...

  2. linq的语法和案例

    本篇逐一介绍linq各个关键字的用法(from,select,group,into等),本篇所有的案例都是用linqpad来完成的(官方地址:http://www.linqpad.net/),建议想学 ...

  3. LINQ简单案例

    1.在visual studio 创建一个解决方案,新建一个控制台程序Kong 2.新建两个类,分别为Master 类和Kongfu类  Master类中包含成员如下,并重写ToString方法 na ...

  4. linq中order by 和group by (含lambda表达式实现)以及综合案例

    一.Linq应用场景 linq的语法通过System.Linq下面的Enumerable类提供支持,也就是说,只要是实现了IEnumerable<T>的对象都可以使用Linq的语法来查询. ...

  5. Linq系列(5)——表达式树之案例应用

    在进入今天的正题之前,先感慨下本人的blog的人气一篇不如一篇.再加上换公司后人身自由受到了比之前大得多得多的限制,实在令本人有些郁闷.不过每次提笔写些东西跟大家分享,总是能让我感到愉悦和欣慰,希望我 ...

  6. Linq查询案例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. linq详细案例

    LINQ to SQL语句(1)之Where 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句.Where操 ...

  8. Microsoft Visual Studio 2017 for Mac Preview 下载+安装+案例Demo

    目录: 0. 前言 1. 在线安装器 2. 安装VS 3. HelloWorld 4. ASP.NET MVC 5. 软件下载 6. 结尾 0. 前言: 工作原因,上下班背着我的雷神,一个月瘦了10斤 ...

  9. Redis简单案例(一) 网站搜索的热搜词

    对于一个网站来说,无论是商城网站还是门户网站,搜索框都是有一个比较重要的地位,它的存在可以说是 为了让用户更快.更方便的去找到自己想要的东西.对于经常逛这个网站的用户,当然也会想知道在这里比较“火” ...

随机推荐

  1. 网络通信-ping命令

  2. 我的Spring MVC第一个应用

    Product package com.mstf.bean; import java.io.Serializable; /** * Product类,封装了一些信息,包含三个属性 * @author ...

  3. 浅谈Sass与Less区别、优缺点

    Sass是一种动态样式语言,Sass语法的缩排语法,比Css比多出很多功能,如变量,嵌套,运算,继承,颜色处理,函数等,易于阅读.Cass的安装需要安装Ruby环境,是服务器端处理的,Less是需要引 ...

  4. docker for centos7

    docker for centos7 据官方所说,docker在新版本的ubuntu和centos7上表现更好,鉴于我们目前使用的系统是centos6.8,这次我们选择centos7作为docker的 ...

  5. HTML学习----------DAY2第四节

    HTML 文档是由 HTML 元素定义的. HTML 元素 HTML 元素指的是从开始标签(start tag)到结束标签(end tag)的所有代码. 注释:开始标签常被称为开放标签(opening ...

  6. 洛谷 P2440 木材加工

    P2440 木材加工 题目背景 要保护环境 题目描述 题目描述: 木材厂有一些原木,现在想把这些木头切割成一些长度相同的小段木头(木头有可能有 剩余),需要得到的小段的数目是给定的.当然,我们希望得到 ...

  7. leetcode笔记:Range Sum Query - Mutable

    一. 题目描写叙述 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...

  8. 在启动php时,无法启动此程序,由于计算机中丢失MSVCR110.dll的解决方法

    在启动php时,运行RunHiddenconsole.exe php-cgi.exe -b 127.0.0.1:9000 -c时,出现错误:无法启动此程序,由于计算机中丢失MSVCR110.dll 方 ...

  9. 异步FIFO及verilog原码

    这几天看了Clifford E. Cummings的两篇大作<Simulation and Synthesis Techniques for Asynchronous FIFO Design&g ...

  10. POJ 1671 第二类斯特林数

    思路: 递推出来斯特林数 求个和 if(i==j)f[i][j]=1; else f[i][j]=f[i-1][j-1]+f[i-1][j]*j; //By SiriusRen #include &l ...