• 看Linq代码产生的震撼


    1. 实体类可以这样定义
    public class PersonInfo
        {
            
    public bool IsOnline { getset; }
            
    public string Name { getset; }
            
    public int Id { getset; }
        }


    2. 数组可以这样操作
    string[] words = { "hello""wonderful""linq""beautiful""world" };

        
    // Get only short words
        var shortWords =
          from word 
    in words
          
    where word.Length <= 5
          select word;

        
    // Print each word out
        foreach (var word in shortWords)
          Console.WriteLine(word);


    3. 匿名类型(anonymous type)
    var books = new[] {
          
    new {Title="Ajax in Action", Publisher="Manning", Year=2005 },
          
    new {Title="Windows Forms in Action", Publisher="Manning", Year=2006 },
          
    new {Title="RSS and Atom in Action", Publisher="Manning", Year=2006 }
        };


    4. XML可以这样操作
    //对象books在3中初始化
    XElement xml = new XElement("books",
          from book 
    in books
          
    where book.Year == 2006
          select 
    new XElement("book",
            
    new XAttribute("title", book.Title),
            
    new XElement("publisher", book.Publisher)
          )
        );


    5. 支持比较复杂的分组操作
    string[] words = { "hello""wonderful""linq""beautiful""world" };

        
    // Group words by length
        var groups =
          from word 
    in words
          orderby word ascending
          group word by word.Length into lengthGroups
          orderby lengthGroups.Key descending
          select 
    new { Length = lengthGroups.Key, Words = lengthGroups };

        
    // Print each group out
        foreach (var group in groups)
        {
          Console.WriteLine(
    "Words of length " + group.Length);
          
    foreach (string word in group.Words)
            Console.WriteLine(
    "  " + word);
        }



    http://code.google.com/p/linqinaction-csharp-sample/
  • 相关阅读:
    [转发]UML类图符号 各种关系说明以及举例
    Promise 对象
    ES6基础(二)
    ES6基础
    JSON介绍
    Ajax的面试题
    Ajax请求
    jQuery从小白开始---初始jQuery
    常用的String原型
    JS之类数组
  • 原文地址:https://www.cnblogs.com/gaotianpu/p/1378484.html
Copyright © 2020-2023  润新知