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之类的东西开始, 但很难用那些东西说出好玩的东西. 这次选择的是一个不太出名但很有 ...
随机推荐
- Egret 双端接入爱贝支付遇到的问题
首先要为 egret 工程引入第三方库: Egret 接第三方库:http://edn.egret.com/cn/index.php?g=&m=article&a=index& ...
- BZOJ 1492 货币兑换
Description Input 第一行两个正整数\(N,S\),分别表示小Y 能预知的天数以及初始时拥有的钱数. 接下来\(N\)行,第\(K\)行三个实数\(A_{K},B_{K},Rate_{ ...
- BZOJ 3243 向量内积
Description 两个\(d\)维向量\(A=[a_{1},a_{2},...,a_{d}]\)与\(B=[b_{1},b_{2},...,b_{d}]\)的内积为其相对应维度的权值的乘积和,即 ...
- cf B Inna and Candy Boxes
题意:输入n,然后输入n个数ai,再输入n个数bi,如果在1-ai中能找到两个数x,y,x和y可以相等,如果x+y=bi,答案加上x*y,否则减去1,让结果尽可能大,输出结果. #include &l ...
- 宏中"#"和"##"的用法
一.一般用法 我们使用#把宏参数变为一个字符串,用##把两个宏参数贴合在一起. 用法: #include<cstdio> #include<climits> using nam ...
- Lua开发环境配置
Lua(英语发音:/ˈluːə/)程序设计语言是一个简洁.轻量.可扩展的脚本语言,是葡萄牙语中“Luna”(月亮)的意思. Lua is a powerful, fast, lightweight, ...
- InnoDB这种行锁实现特点意味者:只有通过索引条件检索数据,InnoDB才会使用行级锁,否则,InnoDB将使用表锁!
InnoDB行锁是通过索引上的索引项来实现的,这一点MySQL与Oracle不同,后者是通过在数据中对相应数据行加锁来实现的. InnoDB这种行锁实现特点意味者:只有通过索引条件检索数据,InnoD ...
- jquery禁用右键、文本选择功能、刷新
//禁用右键.文本选择功能.刷新 $(document).bind(“contextmenu”,function(){return false;}); $(document).bind(“select ...
- -_-#【jQuery插件】Spinner 数字选择器
Spinner (jQuery UI)
- HDU 5416 CRB and Tree
题目大意: T, T组测试数据 给你一个n有n个点,下标是从 1 开始的.这个是一棵树,然后下面是n-1条边, 每条边的信息是 s,e,w 代表 s-e的权值是w 然后是一个Q代表Q次询问. 每次询问 ...