Linq101-Projection
using System;
using System.Linq; namespace Linq101
{
class Projection
{
/// <summary>
/// This sample uses select to produce a sequence of ints one higher than those in an existing array of ints.
/// </summary>
public void Linq6()
{
int[] numbers = { , , , , , , , , , }; var query = from n in numbers
select n + ; Console.WriteLine("Numbers + 1 :");
foreach (var i in query)
{
Console.WriteLine(i);
}
} /// <summary>
/// This sample uses select to return a sequence of just the names of a list of products.
/// </summary>
public void Linq7()
{
var products = Data.GetProductList(); var query = from p in products
select p.ProductName; Console.WriteLine("Product Names:");
foreach (var productName in query)
{
Console.WriteLine(productName);
}
} /// <summary>
/// This sample uses select to produce a sequence of strings representing the text version of a sequence of ints.
/// </summary>
public void Linq8()
{
int[] numbers = { , , , , , , , , , };
string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var query = from n in numbers
select strings[n]; Console.WriteLine("Number strings:");
foreach (var s in query)
{
Console.WriteLine(s);
}
} /// <summary>
/// This sample uses select to produce a sequence of the uppercase and lowercase versions of each word in the original array.
/// </summary>
public void Linq9()
{
string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" }; var query = from w in words
select new { U = w.ToUpper(), L = w.ToLower() }; Console.WriteLine("Results:");
foreach (var item in query)
{
Console.WriteLine("Uppercase:{0},Lowercase:{1}", item.U, item.L);
}
} /// <summary>
/// This sample uses select to produce a sequence containing text representations of digits and whether their length? is even or odd.
/// </summary>
public void Linq10()
{
int[] numbers = { , , , , , , , , , };
string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var query = from n in numbers
select new { Digit = strings[n], EorO = n % == ? "Even" : "Odd" }; Console.WriteLine("Results:");
foreach (var item in query)
{
Console.WriteLine("The digit {0} is {1}", item.Digit, item.EorO);
}
} /// <summary>
/// This sample uses select to produce a sequence containing some properties of Products, including UnitPrice which is renamed to Price in the resulting type.
/// </summary>
public void Linq11()
{
var products = Data.GetProductList(); var query = from p in products
select new { p.ProductName, p.Category, Price = p.UnitPrice }; Console.WriteLine("Product Info:");
foreach (var product in query)
{
Console.WriteLine("{0} is in the category {1} and cost {2} per unit", product.ProductName, product.Category, product.Price);
}
} /// <summary>
/// This sample uses an indexed Select clause to determine if the value of ints in an array match their position in the array.
/// </summary>
public void Linq12()
{
int[] numbers = { , , , , , , , , , }; var query = numbers.Select((n, index) => new { Num = n, InPlace = n == index }); Console.WriteLine("Number:In-place?");
foreach (var number in query)
{
Console.WriteLine("{0}:{1}", number.Num, number.InPlace);
}
} /// <summary>
/// This sample combines select and where to make a simple query that returns the text form of each digit less than 5.
/// </summary>
public void Linq13()
{
int[] numbers = { , , , , , , , , , };
string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var query = from n in numbers
where n <
select digits[n]; Console.WriteLine("Numbers < 5:");
foreach (var digit in query)
{
Console.WriteLine(digit);
}
} /// <summary>
/// This sample uses a compound from clause to make a query that returns all pairs of numbers from both arrays such that the number from numbersA is less than the number from numbersB.
/// </summary>
public void Linq14()
{
int[] numbersA = { , , , , , , };
int[] numbersB = { , , , , }; var query = from a in numbersA
from b in numbersB
where a < b
select new { a, b }; Console.WriteLine("Pairs where a < b");
foreach (var pair in query)
{
Console.WriteLine("{0} is less than {1}", pair.a, pair.b);
}
} /// <summary>
/// This sample uses a compound from clause to select all orders where the order total is less than 500.00.
/// </summary>
public void Linq15()
{
var customers = Data.GetCustomerList(); var query = from c in customers
from o in c.Orders
where o.Total <
select new { c.CustomerID, o.OrderID, o.Total }; ObjectDumper.Write(query);
} /// <summary>
/// This sample uses a compound from clause to select all orders where the order was made in 1998 or later.
/// </summary>
public void Linq16()
{
var customers = Data.GetCustomerList(); var query = from c in customers
from o in c.Orders
where o.OrderDate >= new DateTime(, , )
select new { c.CustomerID, o.OrderID, o.OrderDate }; ObjectDumper.Write(query);
} /// <summary>
/// This sample uses a compound from clause to select all orders where the order total is greater than 2000.00 and uses from assignment to avoid requesting the total twice.
/// </summary>
public void Linq17()
{
var customers = Data.GetCustomerList(); var query = from c in customers
from o in c.Orders
where o.Total >
select new { c.CustomerID, o.OrderID, o.Total }; ObjectDumper.Write(query);
} /// <summary>
/// This sample uses multiple from clauses so that filtering on customers can be done before selecting their orders. This makes the query more efficient by not selecting and then discarding orders for customers outside of Washington.
/// </summary>
public void Linq18()
{
var customers = Data.GetCustomerList(); //效率低
//var query = from c in customers
// from o in c.Orders
// where c.Region == "WA" && o.OrderDate >= new DateTime(1997, 1, 1)
// select new { c.CustomerID, o.OrderID }; var query = from c in customers
where c.Region == "WA"
from o in c.Orders
where o.OrderDate >= new DateTime(, , )
select new { c.CustomerID, o.OrderID }; ObjectDumper.Write(query);
} /// <summary>
/// This sample uses an indexed SelectMany clause to select all orders, while referring to customers by the order in which they are returned from the query.
/// </summary>
public void Linq19()
{
var customers = Data.GetCustomerList(); //var query = customers.SelectMany(c => c.Orders);
//var query = customers.SelectMany(c => c.Orders).Where(o => o.OrderDate >= new DateTime(1998, 1, 1));
var query =
customers.SelectMany(
(customer, index) =>
(customer.Orders.Select(o => "Customer #" + (index + ) + " has an order with OrderID " + o.OrderID))); ObjectDumper.Write(query);
}
}
}
Linq101-Projection的更多相关文章
- OpenCASCADE BRep Projection
OpenCASCADE BRep Projection eryar@163.com 一网友发邮件问我下图所示的效果如何在OpenCASCADE中实现,我的想法是先构造出螺旋线,再将螺旋线投影到面上. ...
- 鱼眼模式(Fisheye projection)的软件实现
简单实现 鱼眼模式(Fisheye)和普通的透视投影(Perspective projection),一个很大的区别就是鱼眼的投影算法是非线性的(non-linear),实际照相机的情况是在镜头外面包 ...
- 细谈Slick(6)- Projection:ProvenShape,强类型的Query结果类型
在Slick官方文档中描述:连接后台数据库后,需要通过定义Projection,即def * 来进行具体库表列column的选择和排序.通过Projection我们可以选择库表中部分列.也可以增加一些 ...
- <转载> OpenGL Projection Matrix
原文 OpenGL Projection Matrix Related Topics: OpenGL Transformation Overview Perspective Projection Or ...
- (转)投影矩阵的推导(Deriving Projection Matrices)
转自:http://blog.csdn.net/gggg_ggg/article/details/45969499 本文乃<投影矩阵的推导>译文,原文地址为: http://www.cod ...
- inconsistent line count calculation in projection snapshot
1.现象 在vs2013中,按Ctrl + E + D格式化.cshtml代码,vs2013系统崩溃.报:inconsistent line count calculation in projecti ...
- CAF(C++ actor framework)使用随笔(projection 用法)(一)
最近干活在写毕设,用到了CAF,看了文档,发现了一些小坑,自己摸索写点随笔.(CAF的github网站 https://github.com/actor-framework/actor-framewo ...
- shadow projection
1.概述 shadow projection,又可成为planar shadow, 这是一种非常简单的绘制阴影的方法. 主要应用的应用场景:物体在平面投射阴影. 主要思想:把阴影看作是物体在平面上的投 ...
- 重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding
原文:重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding [源码下 ...
- Machine Learning/Random Projection
这次突然打算写点dimension reduction的东西, 虽然可以从PCA, manifold learning之类的东西开始, 但很难用那些东西说出好玩的东西. 这次选择的是一个不太出名但很有 ...
随机推荐
- 10条PHP高级技巧
1.使用一个SQL注射备忘单 一个基本的原则就是,永远不要相信用户提交的数据. 另一个规则就是,在你发送或者存储数据时对它进行转义(escape). 可以总结为:filter input, escap ...
- Oracle问题解决(sqlplus无法登陆)
命令行 sqlplus 无法登陆,常常是用户名/密码错误.监听配置错误或未启动.数据库服务名丢失等等原因. 用户名/密码错误 找到自己设的密码 这全靠自己创建数据库实例时,备份或记住相关信息 若最后没 ...
- [转]加盐hash保存密码的正确方式
0x00 背景 大多数的web开发者都会遇到设计用户账号系统的需求.账号系统最重要的一个方面就是如何保护用户的密码.一些大公司的用户数据库泄露事件也时有发生,所以我们必须采取一些措施来保护用户的密码, ...
- BZOJ 1043 下落的圆盘
Description 有n个圆盘从天而降,后面落下的可以盖住前面的.求最后形成的封闭区域的周长.看下面这副图, 所有的红色线条的总长度即为所求. Input n ri xi y1 ... rn x ...
- ExtJs3常用控件操作实例
结合工作内容,不定期更新.这里面可能会讲到一些常用的组件的操作. json: { "total": 30, "data": [{ "funcAlign ...
- C语言宏定义使用技巧
写好C语言,漂亮的宏定义很重要,使用宏定义可以防止出错,提高可移植性,可读性,方便性 等等.下面列举一些成熟软件中常用得宏定义...... 1.防止一个头文件被重复包含 #ifndef COMDEF_ ...
- Linux 统计文件夹下文件个数
查看统计当前目录下文件的个数,包括子目录里的. ls -lR| grep "^-" | wc -l Linux下查看某个目录下的文件.或文件夹个数用到3个命令:ls列目录.用gre ...
- The Same Game(模拟)
http://poj.org/problem?id=1027 题意:给一个10*15的地图,里面填充R,G,B三种颜色,每次找到当前地图的同色最大区域M,并将其删除,删除M后,上面的小球自然下落,当有 ...
- oracle存储过程 --1
一,oracle存储过程语法 1.oracle存储过程结构 CREATE OR REPLACE PROCEDURE oracle存储过程名字 ( 参数1 IN NUMBER, 参 ...
- 在线LCA模板
在线LCA 如求A,B两点的LCA,先计算出各个结点的深度d[],然后,通过递推公式求出各个结点的2次方倍的祖先p[],假设d[A] > d[B],则找到d[p[A][i]] == d[B]也就 ...