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之类的东西开始, 但很难用那些东西说出好玩的东西. 这次选择的是一个不太出名但很有 ...
随机推荐
- Unity3D Quaternion各属性和函数测试
Quaternion属性与方法 一,属性: x.y.z就不说了,只看一个eulerAngles,代码如下: public Quaternion rotation = Quaternion.identi ...
- BZOJ 1483 梦幻布丁
Description \(N\)个布丁摆成一行,进行\(M\)次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为\(1,2,2,1\)的四个布丁一共有\ ...
- Unity KGFMapSystem插件制作小地图
KGFMapSystem版本:2.3 在我们开发游戏或者虚拟现实中,一般都会用到小地图,如果要我们去写小地图,可以用到unity 3d中就有一个插件,是专门开发小地图用的,这个插件就是KGFMapSy ...
- python对拍程序
import sys; import random; import os; gen=open("data.in","w"); #///生成测试数据 gen.cl ...
- Linux Shell编程(9)——特殊变量类型
局部变量局部变量只在代码块或一个函数里有效 (参考函数里的局部变量)环境变量这种变量会影响Shell的行为和用户接口 在大多数情况下,每个进程都会有一个"环境表", 它由一组由进程 ...
- C语言基础课程 第二课 HelloWorld不为菜鸟所知的秘密
1 愉快的开端hello world 4 1.1 include头文件包含 4 1.2 main函数 4 1.3 注释 4 1.4 {}括号,程序 ...
- 【动态规划】【二分】【最长上升子序列】HDU 5773 The All-purpose Zero
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5773 题目大意: T组数据,n个数(n<=100000),求最长上升子序列长度(0可以替代任何 ...
- TXT四则运算计算器 后日谈
经过了软件工程第一个个人项目——<<四则运算器>>的开发后,对软件开发有了新的认识.题目中并没有明确说明对小数和负数是否应该提供支持.在第一个项目结束后,第二个项目则是针对上一 ...
- nyoj 222 整数中的1个数以及这类问题
之前也写过一篇这样的文章,但是隔了这么久,竟然忘了.还是要有清晰的思路,才能真正的掌握. 这道题是这样的: 给出两个非负32位整型范围内的数a,b,请输出闭区间[a,b]内所有数二进制中各个位的1的总 ...
- 如何高性能的给UIImageView加个圆角
文/natewang(简书作者)原文链接:http://www.jianshu.com/p/268f3839d2e6著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 其实你只需要的是圆角 ...