class IntroToLINQ
{
static void Main()
{
// The Three Parts of a LINQ Query:
// 1. Data source.
int[] numbers = new int[] { , , , , , , }; // 2. Query creation.
// numQuery is an IEnumerable<int>
var numQuery =
from num in numbers
where (num % ) ==
select num; // 3. Query execution.
foreach (int num in numQuery)
{
Console.Write("{0,1} ", num);
}
}
}
Northwnd db = new Northwnd(@"c:\northwnd.mdf");

// Query for customers in London.
IQueryable<Customer> custQuery =
from cust in db.Customers
where cust.City == "London"
select cust;
List<int> numQuery2 =
(from num in numbers
where (num % ) ==
select num).ToList(); // or like this:
// numQuery3 is still an int[] var numQuery3 =
(from num in numbers
where (num % ) ==
select num).ToArray();
var queryLondonCustomers = from cust in customers
where cust.City == "London"
select cust;
// queryCustomersByCity is an IEnumerable<IGrouping<string, Customer>>
var queryCustomersByCity =
from cust in customers
group cust by cust.City; // customerGroup is an IGrouping<string, Customer>
foreach (var customerGroup in queryCustomersByCity)
{
Console.WriteLine(customerGroup.Key);
foreach (Customer customer in customerGroup)
{
Console.WriteLine(" {0}", customer.Name);
}
}
class Student
{
public string First { get; set; }
public string Last {get; set;}
public int ID { get; set; }
public string Street { get; set; }
public string City { get; set; }
public List<int> Scores;
} class Teacher
{
public string First { get; set; }
public string Last { get; set; }
public int ID { get; set; }
public string City { get; set; }
}
class DataTransformations
{
static void Main()
{
// Create the first data source.
List<Student> students = new List<Student>()
{
new Student {First="Svetlana",
Last="Omelchenko",
ID=,
Street="123 Main Street",
City="Seattle",
Scores= new List<int> {, , , }},
new Student {First="Claire",
Last="O’Donnell",
ID=,
Street="124 Main Street",
City="Redmond",
Scores= new List<int> {, , , }},
new Student {First="Sven",
Last="Mortensen",
ID=,
Street="125 Main Street",
City="Lake City",
Scores= new List<int> {, , , }},
}; // Create the second data source.
List<Teacher> teachers = new List<Teacher>()
{
new Teacher {First="Ann", Last="Beebe", ID=, City = "Seattle"},
new Teacher {First="Alex", Last="Robinson", ID=, City = "Redmond"},
new Teacher {First="Michiyo", Last="Sato", ID=, City = "Tacoma"}
}; // Create the query.
var peopleInSeattle = (from student in students
where student.City == "Seattle"
select student.Last)
.Concat(from teacher in teachers
where teacher.City == "Seattle"
select teacher.Last); Console.WriteLine("The following students and teachers live in Seattle:");
// Execute the query.
foreach (var person in peopleInSeattle)
{
Console.WriteLine(person);
} Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
/* Output:
The following students and teachers live in Seattle:
Omelchenko
Beebe
*/
var query = from cust in Customers
select cust.City;
var query = from cust in Customer
select new {Name = cust.Name, City = cust.City};
class XMLTransform
{
static void Main()
{
// Create the data source by using a collection initializer.
// The Student class was defined previously in this topic.
List<Student> students = new List<Student>()
{
new Student {First="Svetlana", Last="Omelchenko", ID=, Scores = new List<int>{, , , }},
new Student {First="Claire", Last="O’Donnell", ID=, Scores = new List<int>{, , , }},
new Student {First="Sven", Last="Mortensen", ID=, Scores = new List<int>{, , , }},
}; // Create the query.
var studentsToXML = new XElement("Root",
from student in students
let x = String.Format("{0},{1},{2},{3}", student.Scores[],
student.Scores[], student.Scores[], student.Scores[])
select new XElement("student",
new XElement("First", student.First),
new XElement("Last", student.Last),
new XElement("Scores", x)
) // end "student"
); // end "Root" // Execute the query.
Console.WriteLine(studentsToXML); // Keep the console open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
< Root>
<student>
<First>Svetlana</First>
<Last>Omelchenko</Last>
<Scores>,,,</Scores>
</student>
<student>
<First>Claire</First>
<Last>O'Donnell</Last>
<Scores>,,,</Scores>
</student>
<student>
<First>Sven</First>
<Last>Mortensen</Last>
<Scores>,,,</Scores>
</student>
</Root>
class FormatQuery
{
static void Main()
{
// Data source.
double[] radii = { , , }; // Query.
IEnumerable<string> query =
from rad in radii
select String.Format("Area = {0}", (rad * rad) * 3.14); // Query execution.
foreach (string s in query)
Console.WriteLine(s); // Keep the console open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
/* Output:
Area = 3.14
Area = 12.56
Area = 28.26
*/

http://msdn.microsoft.com/en-us/library/bb397924.aspx

地址;http://msdn.microsoft.com/en-us/library/bb397927.aspx

C#中linq的更多相关文章

  1. MVC中Linq to sql创建数据模型

    1.创建新的 SQL Server 数据库 点击”视图“-->“服务器资源管理器” ,打开 “服务器资源管理器” 窗口,如下图: 右键“数据连接”,选择“创建新的SQL Server 数据库”, ...

  2. C#中Linq查询基本操作

    摘要:本文介绍Linq查询基本操作(查询关键字) - from 子句 - where 子句 - select子句 - group 子句 - into 子句 - orderby 子句 - join 子句 ...

  3. VB.NET中LINQ TO List泛型查询语句(分组,聚合函数)

    Public Class LinqToList 'LINQ在C#中使用比较方便,但是在VB中使用比较麻烦,复杂,和C#用法并不太一样 Dim listNew As List(Of Product) = ...

  4. C#中Linq延迟执行问题

    本文来自:http://msdn.microsoft.com/zh-cn/library/bb399393(v=vs.110).aspx http://www.cnblogs.com/zhanglin ...

  5. Webform中linq to sql多条件查询(小练习)

    多条件查询:逐条判断,从第一个条件开始判断,如果满足,取出放入集合,再从集合中查询第二个条件... aspx代码: <body> <form id="form1" ...

  6. C#中linq报“Character literal must contain exactly one character”的错误提示

    后台代码使用linq提示"Character literal must contain exactly one character": 网上看了一下提示在部分linq语句中直接写入 ...

  7. 在LINQ查询中LINQ之Group By的用法

    LINQ定义了大约40个查询操作符,如select.from.in.where.group 以及order by,借助于LINQ技术,我们可以使用一种类似SQL的语法来查询任何形式的数据.Linq有很 ...

  8. Json.Net 中Linq to JSON的操作

    Linq to JSON是用来操作JSON对象的.可以用于快速查询,修改和创建JSON对象.当JSON对象内容比较复杂,而我们仅仅需要其中的一小部分数据时,可以考虑使用Linq to JSON来读取和 ...

  9. .NET 7 中 LINQ 的疯狂性能提升

    LINQ 是 Language INtegrated Query 单词的首字母缩写,翻译过来是语言集成查询.它为查询跨各种数据源和格式的数据提供了一致的模型,所以叫集成查询.由于这种查询并没有制造新的 ...

  10. Newtonsoft.json中 linq to json 和序列化哪个快?

    Newtonsoft.json是最常用的json序列化组件,当然他不是最快的,但是是功能最全的.. using System; using System.Collections.Generic; us ...

随机推荐

  1. iOS UIwebView html 字符串转换

    解析json字段是一段html串,平常解析出来都能在uiwebview上正常显示,这却出现以下状况,文本内容夹杂好多不需要显示的字符,例如: NSString*string =@"<s ...

  2. 沈逸老师PHP魔鬼特训笔记(1)

    此课程个人开发环境可以考虑使用Ubuntu ,推荐sublime和PhpStorm作为开发环境.一.PHP的一大特性是:脚本语言.不要编译,写完就可以运行? 然而并不是....... PHP代码要想运 ...

  3. 【阿里云产品公测】PTS压力测试WP站搜索

    [阿里云产品公测]PTS压力测试WP站搜索 作者:阿里云用户cnsjw PTS性能测试服务是一个非常非常强大的压力测试工具.可以模拟百人同时访问网站的情况,并监测ECS和RDS的各项指标,生成非常详细 ...

  4. 基于SocketAsyncEventArgs的版本

    文字水平差就慢慢开始练习,同时分享一下,项目中写的简单socket程序,不同方式的版本,今天上一个异步.可能实现高性能的处理方式.IOCP就不多说了,高性能的完成端口,可以实现套接字对象的复用,降低开 ...

  5. android中的一些问题

    1. Android dvm的进程和Linux的进程, 应用程序的进程是否为同一个概念 DVM指dalivk的虚拟机.每一个Android应用程序都在它自己的进程中运行,都拥有一个独立的Dalvik虚 ...

  6. 社交分享:-canOpenURL: failed for URL: "weixin://app/*************/" - error: "This app is not allowed to query for scheme weixin"

    升级到iOS9后,微信,QQ,微博等社交软件的分享都失效了,控制台默默地打印了这条信息: This app is not allowed to query for scheme xxx 这是因为iOS ...

  7. xe5 android sample 中的 SimpleList 是怎样绑定的

    C:\Users\Public\Documents\RAD Studio\12.0\Samples\FireMonkeyMobile 例子中的绑定方式如下图: 1.拖拽一个listview到界面上,然 ...

  8. hdu 3663 DLX

    思路:把每个点拆成(d+1)*n列,行数为可拆分区间数.对所有的有i号点拆分出来的行都要建一条该行到i列的边,那么就能确保有i号点拆出来的行只能选择一行. #include<set> #i ...

  9. HTML CSS编码规范(黄金定律)

    HTML 语法 用两个空格来代替制表符(tab) -- 这是唯一能保证在所有环境下获得一致展现的方法. 嵌套元素应当缩进一次(即两个空格). 对于属性的定义,确保全部使用双引号,绝不要使用单引号. 不 ...

  10. Table of Contents - JMS

    JMS Specification v1.1 JMS 基本概念 Message QueueBrowser 消息选择器 消息确认 ConnectionMetaData ExceptionListener ...