zoukankan      html  css  js  c++  java
  • 看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/
  • 相关阅读:
    关于课程设计、毕业设计的一些总结与思考
    分享一个Panda C-60 维修心得
    未能加载文件或程序集“SuperMap.Data.dll”
    VS2017环境下安装AO10.2的方法
    SQL Server连接错误1326
    VMWare虚拟机中CPU过高的问题
    Apktool编译找不到“keyboardNavigationCluster”
    Aspose.Cells设置单元格格式
    谷歌Chrome浏览器无法安装插件的解决方法
    Global Mapper如何加载在线地图
  • 原文地址:https://www.cnblogs.com/gaotianpu/p/1378484.html
Copyright © 2011-2022 走看看