C#语法中的select
第一次学着用Linq的盆友们,可以看看哈。。。。
代码 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->class SelectSample1
{
static void Main()
{
//Create the data source
List<int> Scores = new List<int>() { , , , }; // Create the query.
IEnumerable<int> queryHighScores =
from score in Scores
where score >
select score; // Execute the query.
foreach (int i in queryHighScores)
{
Console.Write(i + " ");
}
}
}
2、集合序列化:
private XElement SerializeDesignerItems(IEnumerable<DesignerItem> designerItems)
{
XElement serializedItems = new XElement("DesignerItems",
from item in designerItems
let contentXaml = XamlWriter.Save(((DesignerItem)item).Content)
select new XElement("DesignerItem",
new XElement("Left", Canvas.GetLeft(item)),
new XElement("Top", Canvas.GetTop(item)),
new XElement("Width", item.Width),
new XElement("Height", item.Height),
new XElement("ID", item.ID),
new XElement("zIndex", Canvas.GetZIndex(item)),
new XElement("IsGroup", item.IsGroup),
new XElement("ParentID", item.ParentID),
new XElement("Content", contentXaml)
)
); return serializedItems;
}
下面的示例演示了 select 子句可能采用的所有不同形式。在每个查询中,请注意 select 子句和查询变量(studentQuery1、studentQuery2 等)的类型之间的关系。
class SelectSample2
{
// Define some classes
public class Student
{
public string First { get; set; }
public string Last { get; set; }
public int ID { get; set; }
public List<int> Scores;
public ContactInfo GetContactInfo(SelectSample2 app, int id)
{
ContactInfo cInfo =
(from ci in app.contactList
where ci.ID == id
select ci)
.FirstOrDefault(); return cInfo;
} public override string ToString()
{
return First + " " + Last + ":" + ID;
}
} public class ContactInfo
{
public int ID { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public override string ToString() { return Email + "," + Phone; }
} public class ScoreInfo
{
public double Average { get; set; }
public int ID { get; set; }
} // The primary data source
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>() {, , , }},
new Student {First="Cesar", Last="Garcia", ID=, Scores= new List<int>() {, , , }},
}; // Separate data source for contact info.
List<ContactInfo> contactList = new List<ContactInfo>()
{
new ContactInfo {ID=, Email="SvetlanO@Contoso.com", Phone="206-555-0108"},
new ContactInfo {ID=, Email="ClaireO@Contoso.com", Phone="206-555-0298"},
new ContactInfo {ID=, Email="SvenMort@Contoso.com", Phone="206-555-1130"},
new ContactInfo {ID=, Email="CesarGar@Contoso.com", Phone="206-555-0521"}
}; static void Main(string[] args)
{
SelectSample2 app = new SelectSample2(); // Produce a filtered sequence of unmodified Students.
IEnumerable<Student> studentQuery1 =
from student in app.students
where student.ID >
select student; Console.WriteLine("Query1: select range_variable");
foreach (Student s in studentQuery1)
{
Console.WriteLine(s.ToString());
} // Produce a filtered sequence of elements that contain
// only one property of each Student.
IEnumerable<String> studentQuery2 =
from student in app.students
where student.ID >
select student.Last; Console.WriteLine("\r\n studentQuery2: select range_variable.Property");
foreach (string s in studentQuery2)
{
Console.WriteLine(s);
} // Produce a filtered sequence of objects created by
// a method call on each Student.
IEnumerable<ContactInfo> studentQuery3 =
from student in app.students
where student.ID >
select student.GetContactInfo(app, student.ID); Console.WriteLine("\r\n studentQuery3: select range_variable.Method");
foreach (ContactInfo ci in studentQuery3)
{
Console.WriteLine(ci.ToString());
} // Produce a filtered sequence of ints from
// the internal array inside each Student.
IEnumerable<int> studentQuery4 =
from student in app.students
where student.ID >
select student.Scores[]; Console.WriteLine("\r\n studentQuery4: select range_variable[index]");
foreach (int i in studentQuery4)
{
Console.WriteLine("First score = {0}", i);
} // Produce a filtered sequence of doubles
// that are the result of an expression.
IEnumerable<double> studentQuery5 =
from student in app.students
where student.ID >
select student.Scores[] * 1.1; Console.WriteLine("\r\n studentQuery5: select expression");
foreach (double d in studentQuery5)
{
Console.WriteLine("Adjusted first score = {0}", d);
} // Produce a filtered sequence of doubles that are
// the result of a method call.
IEnumerable<double> studentQuery6 =
from student in app.students
where student.ID >
select student.Scores.Average(); Console.WriteLine("\r\n studentQuery6: select expression2");
foreach (double d in studentQuery6)
{
Console.WriteLine("Average = {0}", d);
} // Produce a filtered sequence of anonymous types
// that contain only two properties from each Student.
var studentQuery7 =
from student in app.students
where student.ID >
select new { student.First, student.Last }; Console.WriteLine("\r\n studentQuery7: select new anonymous type");
foreach (var item in studentQuery7)
{
Console.WriteLine("{0}, {1}", item.Last, item.First);
} // Produce a filtered sequence of named objects that contain
// a method return value and a property from each Student.
// Use named types if you need to pass the query variable
// across a method boundary.
IEnumerable<ScoreInfo> studentQuery8 =
from student in app.students
where student.ID >
select new ScoreInfo
{
Average = student.Scores.Average(),
ID = student.ID
}; Console.WriteLine("\r\n studentQuery8: select new named type");
foreach (ScoreInfo si in studentQuery8)
{
Console.WriteLine("ID = {0}, Average = {1}", si.ID, si.Average);
} // Produce a filtered sequence of students who appear on a contact list
// and whose average is greater than 85.
IEnumerable<ContactInfo> studentQuery9 =
from student in app.students
where student.Scores.Average() >
join ci in app.contactList on student.ID equals ci.ID
select ci; Console.WriteLine("\r\n studentQuery9: select result of join clause");
foreach (ContactInfo ci in studentQuery9)
{
Console.WriteLine("ID = {0}, Email = {1}", ci.ID, ci.Email);
} // Keep the console window open in debug mode
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
/* Output
Query1: select range_variable
Claire O'Donnell:112
Sven Mortensen:113
Cesar Garcia:114 studentQuery2: select range_variable.Property
O'Donnell
Mortensen
Garcia studentQuery3: select range_variable.Method
ClaireO@Contoso.com,206-555-0298
SvenMort@Contoso.com,206-555-1130
CesarGar@Contoso.com,206-555-0521 studentQuery4: select range_variable[index]
First score = 75
First score = 88
First score = 97 studentQuery5: select expression
Adjusted first score = 82.5
Adjusted first score = 96.8
Adjusted first score = 106.7 studentQuery6: select expression2
Average = 72.25
Average = 84.5
Average = 88.25 studentQuery7: select new anonymous type
O'Donnell, Claire
Mortensen, Sven
Garcia, Cesar studentQuery8: select new named type
ID = 112, Average = 72.25
ID = 113, Average = 84.5
ID = 114, Average = 88.25 studentQuery9: select result of join clause
ID = 114, Email = CesarGar@Contoso.com
*/
C#语法中的select的更多相关文章
- update中加入select最常用的update语法
update中加入select最常用的update语法 (转) (2010-08-20 11:40:16) 转载▼ 标签: it 分类: SQL 最常用的update语法是:UPDATE <ta ...
- SQL语法基础之SELECT
SQL语法基础之SELECT 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.SELECT查看帮助信息 1>.查看SELECT命令的帮助信息 mysql> ? SEL ...
- SQLite中的SELECT子句使用通配符
SQLite中的SELECT子句使用通配符 在SELECT子句中支持两个通配符,分别为*和table_name.*.其中,*通配符会将FROM子句中每个源表的所有用户定义的列进行输出.例如,下面的SQ ...
- SQLite中的SELECT子句使用别名
SQLite中的SELECT子句使用别名 开发者可以使用AS关键字为指定的列名提供一个新的别名,其语法形式如下 SELECT column_name AS Alias [,…] 例如,下面的SQL语句 ...
- oracle 中 insert select 和 select insert 配合使用
Insert Into select 与 Select Into 哪个更快? 在平常数据库操作的时候,我们有时候会遇到表之间数据复制的情况,可能会用到INSERT INTO SELECT 或者 SEL ...
- 4.InfluxDB-InfluxQL基础语法教程--基本select语句
本文翻译自官网,官网地址:(https://docs.influxdata.com/influxdb/v1.7/query_language/data_exploration/) 基本语法如下: SE ...
- mysql: update字段中带select
update字段中带select UPDATE tb_report_type A INNER JOIN (SELECT LEVEL_CODE FROM tb_report_type WHERE id ...
- HTML中的<select>标签如何设置默认选中的选项
方法有两种. 第一种通过<select>的属性来设置选中项,此方法可以在动态语言如php在后台根据需要控制输出结果. 1 2 3 4 5 < select id = " ...
- C#语法中一个问号(?)和两个问号(??)的运算符是什么意思?
(1).C#语法中一个个问号(?)的运算符是指:可以为 null 的类型. MSDN上面的解释: 在处理数据库和其他包含不可赋值的元素的数据类型时,将 null 赋值给数值类型或布尔型以及日期类型的功 ...
随机推荐
- Oracle 解决无法生成Snapshot问题
1. 概述 Specify the number of days of snapshots to choose from ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...
- WinDbg 图形界面功能(一)
当我们启动windbg后,我们就能看到Windbg的样子了,如下: 本部分讨论 WinDbg 图形用户界面的元素. 这些元素包括以下各项:菜单.工具栏和快捷键.菜单有:文件菜单.编辑菜单.视图菜单.调 ...
- Optimize Cube.js Performance with Pre-Aggregations
转自:https://cube.dev/blog/high-performance-data-analytics-with-cubejs-pre-aggregations/ 可以了解 Pre-Aggr ...
- CSP-S 复赛之前的任务计划
一. 最短路算法复习 ★1.Dijkstra: 2. SPFA: 3. Floyd: 二. DP 复习 ★1.背包问题: 2.区间 DP: 3.状压 DP: 三. 数据结构 ★1. 线段树: 2. 树 ...
- LDA算法 (主题模型算法) 学习笔记
转载请注明出处: http://www.cnblogs.com/gufeiyang 随着互联网的发展,文本分析越来越受到重视.由于文本格式的复杂性,人们往往很难直接利用文本进行分析.因此一些将文本数值 ...
- spark-shell启动错误
18/06/24 16:41:40 ERROR spark.SparkContext: Error initializing SparkContext.java.net.BindException: ...
- Mybatis传参- 被逗号分割的字符串
String ids = "1,2,3,4,5,6",如ids作为参数传递,查询list返回.mybatis用foreach处理并返回. SELECT * FROM yp_popu ...
- postgresql 字段sql语句 更改表名
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/sq8706/article/detail ...
- 后端小白的Bootstrap笔记 一
栅格系统 下面这张图是Bootstrap对栅格系统有关系数的规定 什么是栅格体统? 栅格系统是Bootstrap提供的移动优先的网格系统, 各个分界点如上: 576px 720px 992px 120 ...
- 迁移mysql数据位置
查看位置: show variables like '%datadir%'; /var/lib/mysql