第一次学着用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的更多相关文章

  1. update中加入select最常用的update语法

    update中加入select最常用的update语法 (转) (2010-08-20 11:40:16) 转载▼ 标签: it 分类: SQL 最常用的update语法是:UPDATE <ta ...

  2. SQL语法基础之SELECT

    SQL语法基础之SELECT 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.SELECT查看帮助信息 1>.查看SELECT命令的帮助信息 mysql> ? SEL ...

  3. SQLite中的SELECT子句使用通配符

    SQLite中的SELECT子句使用通配符 在SELECT子句中支持两个通配符,分别为*和table_name.*.其中,*通配符会将FROM子句中每个源表的所有用户定义的列进行输出.例如,下面的SQ ...

  4. SQLite中的SELECT子句使用别名

    SQLite中的SELECT子句使用别名 开发者可以使用AS关键字为指定的列名提供一个新的别名,其语法形式如下 SELECT column_name AS Alias [,…] 例如,下面的SQL语句 ...

  5. oracle 中 insert select 和 select insert 配合使用

    Insert Into select 与 Select Into 哪个更快? 在平常数据库操作的时候,我们有时候会遇到表之间数据复制的情况,可能会用到INSERT INTO SELECT 或者 SEL ...

  6. 4.InfluxDB-InfluxQL基础语法教程--基本select语句

    本文翻译自官网,官网地址:(https://docs.influxdata.com/influxdb/v1.7/query_language/data_exploration/) 基本语法如下: SE ...

  7. mysql: update字段中带select

    update字段中带select UPDATE tb_report_type A INNER JOIN (SELECT LEVEL_CODE FROM tb_report_type WHERE id ...

  8. HTML中的<select>标签如何设置默认选中的选项

    方法有两种. 第一种通过<select>的属性来设置选中项,此方法可以在动态语言如php在后台根据需要控制输出结果. 1 2 3 4 5 < select  id =  " ...

  9. C#语法中一个问号(?)和两个问号(??)的运算符是什么意思?

    (1).C#语法中一个个问号(?)的运算符是指:可以为 null 的类型. MSDN上面的解释: 在处理数据库和其他包含不可赋值的元素的数据类型时,将 null 赋值给数值类型或布尔型以及日期类型的功 ...

随机推荐

  1. js清空数组

    js-清空array数组 两种实现方式: 1.splice:删除元素并添加新元素,直接对数组进行修改,返回含有被删除元素的数组. arrayObject.splice(index,howmany,el ...

  2. LOJ2327 「清华集训 2017」福若格斯 【不平等博弈】

    题目链接:LOJ 对于这道题,我们要分3步来做它. 什么是 Surreal Number 及如何解决博弈问题. 如何用 Surreal Number 解决这道题. 推出结论之后如何计数 首先看看这篇文 ...

  3. 生成ROM时修改参数

    在Xilinx ISE中生成ROM后,如果修改参数,可能会出现错误: “The Memory Initialization vector can contain between 1 to Write” ...

  4. 下载svn

    http://subversion.apache.org/download.cgi?update=201708081800 Windows下载zip,其他系统的下载tar.gz

  5. 数据结构Java版之邻接表实现图(十)

    邻接表实现图,实际上是在一个数组里面存放链表,链表存放的是连接当前节点的其他节点. package mygraph; import java.util.ArrayList; import java.u ...

  6. 对okhttp参数的一些思考

    背景 项目中使用OkHttp访问三方服务 参数 创建okhttp客户端类的时候需要设置一些参数,有些可能是坑,仅供参考: client = new OkHttpClient.Builder() .di ...

  7. 【Vue.js游戏机实战】- Vue.js实现大转盘抽奖总结

    大家好!先上图看看本次案例的整体效果. 实现思路: Vue component实现大转盘组件,可以嵌套到任意要使用的页面. css3 transform控制大转盘抽奖过程的动画效果. 抽奖组件内使用钩 ...

  8. V语言 基本使用

    新手必看-如何安装配置vlang运行环境(linux,macOS篇) 前置条件 发稿截止前只有Linux 或者 macOS系统能编译通过. 你需要安装clang或gcc 如果是macOS上需运行xco ...

  9. [Linux] RTC 读写指令及测试程序

    CPU:RK3288 系统:Linux IC:hym8563 在 Linux 系统中,指令 date 和 hwclock 都可以读写时间 date:读写系统时间,写时间需要管理员权限 hwclock: ...

  10. C图形化第一步

    之前的贪吃蛇都是在cmd下实现,每次都要调用cls刷新屏幕,简直是闪瞎了我的狗眼. 度娘得知有一种方法可以避免闪烁,即:双缓冲.原理是先在内存中作图,然后将做好的图复制到前台,同时禁止背景刷新. 主要 ...