C#的LINQ to Object
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Linq; namespace codeTest
{
class Program
{
static void Main(string[] args)
{
//linq : Lauage Intergarted Query
//继承IEnumerable的类,都可以使用linq
//Linq to sql,Linq to xml,Linq to DataSet,Linq to objects
//linq语句在具体调用时,才会执行 toList() Count() List<Customer> Customers = new List<Customer>();
Customers.Add(new Customer() { CustomerName = "Li1", CityID = });
Customers.Add(new Customer() { CustomerName = "Li2", CityID = });
Customers.Add(new Customer() { CustomerName = "Li3", CityID = });
Customers.Add(new Customer() { CustomerName = "Li4", CityID = }); List<City> Citys = new List<City>();
Citys.Add(new City() { CityID = , CityName = "北京" });
Citys.Add(new City() { CityID = , CityName = "上海" });
Citys.Add(new City() { CityID = , CityName = "广州" });
Citys.Add(new City() { CityID = , CityName = "深圳" }); #region 简单查询
//1.Query systax
var query = from c in Customers
where c.CityID % ==
orderby c.CityID descending
select c; foreach (var item in query)
{
Console.WriteLine("{0},{1}", item.CustomerName, item.CityID);
} Console.WriteLine(); //2.Method systax
var query2 = Customers.Where(c => c.CityID % == ).OrderByDescending(c => c.CityID);
foreach (var item in query2)
{
Console.WriteLine("{0},{1}", item.CustomerName, item.CityID);
}
#endregion #region Group
Console.WriteLine("queryGroup");
//1.Query systax
var queryGroup = from c in Customers
group c by c.CityID; foreach (var group in queryGroup)
{
Console.WriteLine("{0}", group.Key);
foreach (var item in group)
{
Console.WriteLine("{0},{1}", item.CustomerName, item.CityID);
}
} Console.WriteLine(); //2.Method systax
var queryGroup2 = Customers.GroupBy(c => c.CityID);
foreach (var group in queryGroup2)
{
Console.WriteLine("{0}", group.Key);
foreach (var item in group)
{
Console.WriteLine("{0},{1}", item.CustomerName, item.CityID);
}
}
#endregion #region into
Console.WriteLine("into");
//1.Query systax
var queryGroupinto = from c in Customers
group c by c.CityID into cGroup
where cGroup.Count() >
select new { CityID = cGroup.Key, Cout = cGroup.Count() }; foreach (var group in queryGroupinto)
{
Console.WriteLine("{0},{1}", group.CityID,group.Cout);
} Console.WriteLine(); //2.Method systax
var queryGroupinto2 = Customers.GroupBy(c => c.CityID).
Where(cGroup => cGroup.Count() > ).
Select(cGroup => new { CityID = cGroup.Key, Cout = cGroup.Count() });
foreach (var group in queryGroupinto2)
{
Console.WriteLine("{0},{1}", group.CityID, group.Cout);
}
#endregion #region join
Console.WriteLine("join");
//1.Query systax
var queryJoin = from c in Customers
join ci in Citys on c.CityID equals ci.CityID
select new { CustomerName = c.CustomerName, c.CityID, ci.CityName };
foreach (var item in queryJoin)
{
Console.WriteLine("{0},{1},{2}", item.CustomerName, item.CityID,item.CityName);
}
Console.WriteLine();
//2.Method systax
var queryJoin2 = Customers.Join(Citys,
c => c.CityID,
ci => ci.CityID,
(c, ci) => new { CustomerName = c.CustomerName, c.CityID, ci.CityName }); foreach (var item in queryJoin2)
{
Console.WriteLine("{0},{1},{2}", item.CustomerName, item.CityID, item.CityName);
}
#endregion Console.ReadLine();
} class Customer
{
public string CustomerName { get; set; }
public int CityID { get; set; }
} class City
{
public int CityID { get; set; }
public string CityName { get; set; }
}
} }
C#的LINQ to Object的更多相关文章
- .NET面试题系列[13] - LINQ to Object
.NET面试题系列目录 名言警句 "C# 3.0所有特性的提出都是更好地为LINQ服务的" - Learning Hard LINQ是Language Integrated Que ...
- LINQ系列:Linq to Object投影操作符
投影是指在将序列中的元素转换为一个自定义形式的操作.投影操作符Select和SelectMany用于选择出赋予了适当功能的值.SelectMany操作符可以处理多个集合. LINQ表达式语法: 1. ...
- LINQ系列:Linq to Object生成操作符
生成操作符从现有序列值中创建新的序列. 1. Empty Empty操作符返回一个指定类型的空集. 1>. 原型定义 public static IEnumerable<TResult& ...
- LINQ系列:Linq to Object转换操作符
转换是指将输入对象的类型转变为序列的动作. 1. AsEnumerable AsEnumerable操作符将查询的输入以IEnumberable(T)类型返回. 2. Cast Cast操作符将IEn ...
- LINQ系列:Linq to Object量词操作符
量词操作符返回一个Boolean值,指示序列中是否存在部分或全部元素符号指定条件.LINQ中量词操作符包括:All.Any和Contains. 1. All All操作符判定在集合中是否所有的值都满足 ...
- LinQ To Object 基本用法
http://www.cnblogs.com/terryzh/archive/2012/11/10/2763538.html LinQ To Object 基本用法 inq的基本语法:var resu ...
- SQO (标准查询运算符)方法 & Linq To Object
#region SQO (标准查询运算符) 方法 #region Where() Find() FindAll() FirstOrDefault()等方法 static void c01where() ...
- 1.解剖Linq to object
LINQ想必大家都不陌生了,它的出现使得我们的代码变得更短.更优雅了.至于LINQ是什么,Linq to object这类的扩展方法到底做了些什么.我们使用的EF是如何实现的(如何解析Expressi ...
- Linq to EF 与Linq to Object 使用心得
大家都知道Linq既可以用来查询数据库对象(我这里指的是Entity FrameWork里的Model对象),也可以用来查询内存中的IEnumerable对象. 两者单独查询时都不会出现什么问题,不过 ...
- Linq to OBJECT延时标准查询操作符
1.Where 操作符用于限定输入集合中的元素,将符合条件的元素组织声称一个序列结果.2.Select 操作符用于根据输入序列中的元素创建相应的输出序列中的元素,输出序列中的元素类型可以与输入序列中 ...
随机推荐
- leetcode 236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- [codeforces 360]A. Levko and Array Recovery
[codeforces 360]A. Levko and Array Recovery 试题描述 Levko loves array a1, a2, ... , an, consisting of i ...
- word20161201
http://baike.baidu.com/link?url=ZTTkA-suMlJNGb2AeNBE2E6MZQZwjkvWXKgmUpeLBIrCfC-k32cGJOJLrtDlLXjsTfkD ...
- bug-android之INSTALL_FAILED_NO_MATCHING_ABIS无法安装在虚拟机
bug描述: 经常在网络上下载一些实例,自己研究 ,运行时不时会出现这个bug: Installation error: INSTALL_FAILED_NO_MATCHING_ABIS bug解决方案 ...
- Codeforces Gym 100114 J. Computer Network
Description 给出一个图,求添加一条边使得添加后的图的桥(割边)最少. Sol Tarjan. 一遍Tarjan求割边. 我们发现连接的两个点一定是这两个点之间的路径上的桥最多,然后就可以贪 ...
- Javascript闭包——懂不懂由你,反正我是懂了
摘要:“如果你不能向一个六岁的孩子解释清楚,那么其实你自己根本就没弄懂.”好吧,我试着向一个27岁的朋友就是JS闭包(JavaScript closure)却彻底失败了. 越来越觉得国内没有教书育人的 ...
- 【GoLang】golang 微服务框架 介绍
原文如下: rpcx是一个类似阿里巴巴 Dubbo 和微博 Motan 的分布式的RPC服务框架,基于Golang net/rpc实现. 谈起分布式的RPC框架,比较出名的是阿里巴巴的dubbo,包括 ...
- PageImpl是不是有问题?
pageable.getOffset() + content.size() : total这个API 感觉没有实现该有的功能!!!
- 浅探委托(delegate)和事件(event)
.NET Framework通过委托提供了一种回调函数机制. internal delegate void FeedBack(Int32 value); 内部委托FeedBack的声明,一个委托要指定 ...
- struts2 复杂参数封装
1.1.1 Struts2中封装复杂类型的数据: 封装到List集合: 页面: 商品名称:<input type="text" name="products[ ...