zoukankan      html  css  js  c++  java
  • C#语法中的select

    1、简单的例子

    代码
    class SelectSample1
    {   
        
    static void Main()
        {           
            
    //Create the data source
            List<int> Scores = new List<int>() { 97928160 };

            
    // Create the query.
            IEnumerable<int> queryHighScores =
                from score 
    in Scores
                
    where score > 80
                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;
            }

    3、下面的示例演示了 select 子句可能采用的所有不同形式。在每个查询中,请注意 select 子句和查询变量(studentQuery1studentQuery2 等)的类型之间的关系。

    代码
        class SelectSample2
        {
            
    // Define some classes
            public class Student
            {
                
    public string First { getset; }
                
    public string Last { getset; }
                
    public int ID { getset; }
                
    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 { getset; }
                
    public string Email { getset; }
                
    public string Phone { getset; }
                
    public override string ToString() { return Email + "," + Phone; }
            }

            
    public class ScoreInfo
            {
                
    public double Average { getset; }
                
    public int ID { getset; }
            }

            
    // The primary data source
            List<Student> students = new List<Student>()
            {
                 
    new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new List<int>() {97928160}},
                 
    new Student {First="Claire", Last="O'Donnell", ID=112, Scores= new List<int>() {75849139}},
                 
    new Student {First="Sven", Last="Mortensen", ID=113, Scores= new List<int>() {88946591}},
                 
    new Student {First="Cesar", Last="Garcia", ID=114, Scores= new List<int>() {97898582}},
            };

            
    // Separate data source for contact info.
            List<ContactInfo> contactList = new List<ContactInfo>()
            {
                
    new ContactInfo {ID=111, Email="SvetlanO@Contoso.com", Phone="206-555-0108"},
                
    new ContactInfo {ID=112, Email="ClaireO@Contoso.com", Phone="206-555-0298"},
                
    new ContactInfo {ID=113, Email="SvenMort@Contoso.com", Phone="206-555-1130"},
                
    new ContactInfo {ID=114, 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 > 111
                    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 > 111
                    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 > 111
                    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 > 111
                    select student.Scores[
    0];

                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 > 111
                    select student.Scores[
    0* 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 > 111
                    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 > 111
                    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 > 111
                    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() > 85
                    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
    */



    (全文完)


    以下为广告部分

    您部署的HTTPS网站安全吗?

    如果您想看下您的网站HTTPS部署的是否安全,花1分钟时间来 myssl.com 检测以下吧。让您的HTTPS网站变得更安全!

    SSL检测评估

    快速了解HTTPS网站安全情况。

    安全评级(A+、A、A-...)、行业合规检测、证书信息查看、证书链信息以及补完、服务器套件信息、证书兼容性检测等。

    SSL证书工具

    安装部署SSL证书变得更方便。

    SSL证书内容查看、SSL证书格式转换、CSR在线生成、SSL私钥加解密、CAA检测等。

    SSL漏洞检测

    让服务器远离SSL证书漏洞侵扰

    TLS ROBOT漏洞检测、心血漏洞检测、FREAK Attack漏洞检测、SSL Poodle漏洞检测、CCS注入漏洞检测。

  • 相关阅读:
    大数据学习day31------spark11-------1. Redis的安装和启动,2 redis客户端 3.Redis的数据类型 4. kafka(安装和常用命令)5.kafka java客户端
    大数据学习day29-----spark09-------1. 练习: 统计店铺按月份的销售额和累计到该月的总销售额(SQL, DSL,RDD) 2. 分组topN的实现(row_number(), rank(), dense_rank()方法的区别)3. spark自定义函数-UDF
    大数据学习day28-----hive03------1. null值处理,子串,拼接,类型转换 2.行转列,列转行 3. 窗口函数(over,lead,lag等函数) 4.rank(行号函数)5. json解析函数 6.jdbc连接hive,企业级调优
    大数据学习----day27----hive02------1. 分桶表以及分桶抽样查询 2. 导出数据 3.Hive数据类型 4 逐行运算查询基本语法(group by用法,原理补充) 5.case when(练习题,多表关联)6 排序
    大数据学习day26----hive01----1hive的简介 2 hive的安装(hive的两种连接方式,后台启动,标准输出,错误输出)3. 数据库的基本操作 4. 建表(内部表和外部表的创建以及应用场景,数据导入,学生、分数sql练习)5.分区表 6加载数据的方式
    大数据学习day25------spark08-----1. 读取数据库的形式创建DataFrame 2. Parquet格式的数据源 3. Orc格式的数据源 4.spark_sql整合hive 5.在IDEA中编写spark程序(用来操作hive) 6. SQL风格和DSL风格以及RDD的形式计算连续登陆三天的用户
    大数据学习day24-------spark07-----1. sortBy是Transformation算子,为什么会触发Action 2. SparkSQL 3. DataFrame的创建 4. DSL风格API语法 5 两种风格(SQL、DSL)计算workcount案例
    大数据学习day23-----spark06--------1. Spark执行流程(知识补充:RDD的依赖关系)2. Repartition和coalesce算子的区别 3.触发多次actions时,速度不一样 4. RDD的深入理解(错误例子,RDD数据是如何获取的)5 购物的相关计算
    大数据学习day22------spark05------1. 学科最受欢迎老师解法补充 2. 自定义排序 3. spark任务执行过程 4. SparkTask的分类 5. Task的序列化 6. Task的多线程问题
    [POJ] 1325 Machine Schedule(最小点覆盖)
  • 原文地址:https://www.cnblogs.com/zhuqil/p/1650649.html
Copyright © 2011-2022 走看看