C#中linq
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的更多相关文章
- MVC中Linq to sql创建数据模型
1.创建新的 SQL Server 数据库 点击”视图“-->“服务器资源管理器” ,打开 “服务器资源管理器” 窗口,如下图: 右键“数据连接”,选择“创建新的SQL Server 数据库”, ...
- C#中Linq查询基本操作
摘要:本文介绍Linq查询基本操作(查询关键字) - from 子句 - where 子句 - select子句 - group 子句 - into 子句 - orderby 子句 - join 子句 ...
- VB.NET中LINQ TO List泛型查询语句(分组,聚合函数)
Public Class LinqToList 'LINQ在C#中使用比较方便,但是在VB中使用比较麻烦,复杂,和C#用法并不太一样 Dim listNew As List(Of Product) = ...
- C#中Linq延迟执行问题
本文来自:http://msdn.microsoft.com/zh-cn/library/bb399393(v=vs.110).aspx http://www.cnblogs.com/zhanglin ...
- Webform中linq to sql多条件查询(小练习)
多条件查询:逐条判断,从第一个条件开始判断,如果满足,取出放入集合,再从集合中查询第二个条件... aspx代码: <body> <form id="form1" ...
- C#中linq报“Character literal must contain exactly one character”的错误提示
后台代码使用linq提示"Character literal must contain exactly one character": 网上看了一下提示在部分linq语句中直接写入 ...
- 在LINQ查询中LINQ之Group By的用法
LINQ定义了大约40个查询操作符,如select.from.in.where.group 以及order by,借助于LINQ技术,我们可以使用一种类似SQL的语法来查询任何形式的数据.Linq有很 ...
- Json.Net 中Linq to JSON的操作
Linq to JSON是用来操作JSON对象的.可以用于快速查询,修改和创建JSON对象.当JSON对象内容比较复杂,而我们仅仅需要其中的一小部分数据时,可以考虑使用Linq to JSON来读取和 ...
- .NET 7 中 LINQ 的疯狂性能提升
LINQ 是 Language INtegrated Query 单词的首字母缩写,翻译过来是语言集成查询.它为查询跨各种数据源和格式的数据提供了一致的模型,所以叫集成查询.由于这种查询并没有制造新的 ...
- Newtonsoft.json中 linq to json 和序列化哪个快?
Newtonsoft.json是最常用的json序列化组件,当然他不是最快的,但是是功能最全的.. using System; using System.Collections.Generic; us ...
随机推荐
- 从源码的角度分析ViewGruop的事件分发
从源码的角度分析ViewGruop的事件分发. 首先我们来探讨一下,什么是ViewGroup?它和普通的View有什么区别? 顾名思义,ViewGroup就是一组View的集合,它包含很多的子View ...
- iOS部分其他知识
1.界面切换传值 (1)使用button进行界面切换 //当页面跳转时系统自动调用,segue连线 - (void)prepareForSegue:(UIStoryboardSegue *)segue ...
- [置顶] 《算法导论》习题解答搬运&&学习笔记 索引目录
开始学习<算法导论>了,虽然是本大部头,可能很难一下子看完,我还是会慢慢地研究的. 课后的习题和思考有些是很有挑战性的题目,我等蒻菜很难独立解决. 然后发现了Google上有挺全的algo ...
- SpringData JPA详解
Spring Data JPA 1. 概述 Spring JPA通过为用户统一创建和销毁EntityManager,进行事务管理,简化JPA的配置等使用户的开发更加简便. Spring Data ...
- Umbraco(4)-Outputting the Document Type Properties(翻译文档)
翻译原文地址:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/umbraco4outputting-the-document-typ ...
- codeforces 680C C. Bear and Prime 100(数论)
题目链接: C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input s ...
- Pascal 语言中约瑟夫问题:幸运观众
[题目]节目主持人准备从n名学生中挑选一名幸运观众,因为大家都想争当幸运观众,老师只好采取这样的办法:全体同学站成一列,由前面往后面依顺序报数.1,2,1,2……报单数的同学退出队伍,剩下的同学向前靠 ...
- ZWave for Arduino
https://sites.google.com/site/arduinozwave/http://www.ebay.com/sch/i.html?_trksid=p2050601.m570.l131 ...
- Codevs 1287 矩阵乘法&&Noi.cn 09:矩阵乘法(矩阵乘法练手题)
1287 矩阵乘法 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 小明最近在为线性代数而头疼, ...
- Macbook之设置Finder显示文件完整路径
终端里输入:defaults write com.apple.finder _FXShowPosixPathInTitle -bool TRUE;killall Finder 回复输入:default ...