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. android取高度

    Rect rect = new Rect();  getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);  int top = r ...

  2. HUE搭配基础

    * HUE搭配基础 首先简单说一下Hue框架的来源:HUE=HadoopUser Experience,看这名字就知道怎么回事了吧,没错,直白来说就是Hadoop用户体验,是一个开源的Apache H ...

  3. Adobe 2015 CC update (Windows/Mac OS) 独立升级包下载 Adobe Photoshop CC (Windows 32bit)

    Adobe CC 2015 Product Updates/Downloads for Windows ** = To access these updates, please first follo ...

  4. [ Java ] [ Eclipse ] content Auto activation triggers

    重點: That plug-in is not necessary any more. Just go to Preferences > Java > Editor > Conten ...

  5. POJ 1144 Network【割顶】

    学习的这一篇:https://www.byvoid.com/blog/biconnect 割顶:对于无向图G,如果删除某个点u后,连通分量数目增加,称u为图的关节点或者割顶 u为割顶的条件: (1)u ...

  6. zabbix 使用自带模板监控mysql

    1.这里可以采用zabbix自带的mysql模版,但是也需要在mysql服务器上准备获取mysql status的脚本chk_mysql.sh,zabbix通过调用这个脚本来获取mysql的运行信息. ...

  7. C#日期控件datetimepicker保存空值方法

    方法一(推荐): 设置datetimepicker的属性ShowCheckBox为true 在窗口初始化时候,添加代码this.datetimepicker1.Checked = false; 保存日 ...

  8. [React] Implement a Higher Order Component with Render Props

    When making a reusable component, you'll find that people often like to have the API they're most fa ...

  9. Linux 下UVC&amp;V4L2技术简单介绍(二)

    通过前文Linux 下UVC&V4L2技术简单介绍(一)我们了解了UVC和V4L2的简单知识. 这里是USB设备的文档描写叙述:http://www.usb.org/developers/do ...

  10. Launcher3实现壁纸居中

    Launcher3的wallpaper显示是动态的,与Launcher预置桌面数目有关,让壁纸居中,仅仅能确保第一次开机时壁纸居中,后面用户改动桌面数目后,就无法达到了.怎样要在默认桌面数目配置时居中 ...