• LINQ学习笔记


     int[] numbers = new int[] { 10, 55, 43, 13, 57, 40, 22, 88 };

    var result =
                    from number in numbers
                    where number % 2 == 0
                    orderby number descending
                    select number
    ;

                foreach (var item in result)
                {
                    Console.Write("{0}\t", item);
                }

    ------------------------------------------------------------------------

    string[] words = { "hello", "wonderful", "linq", "beautiful", "world" };

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

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

    ------------------------------------------------------------------------

    string[] words = { "hello", "wonderful", "linq", "beautiful", "world" };

                var shortWords =
                    from word in words
                    where word.Length <= 5
                    select word;

                foreach (var word in shortWords)
                    Console.WriteLine(word);

    ------------------------------------------------------------------------

    DataContext dataContext = new DataContext(@"Data Source=......");
                Table<T_IC_APP> appsTable = dataContext.GetTable<T_IC_APP>();

                var apps =
                    from app in appsTable
                    where app.APP_NAME.Contains("FF")
                    select app;

                Console.WriteLine(dataContext.GetCommand(apps).CommandText);

                foreach (var item in apps)
                {
                    Console.WriteLine("APP_ID: {0}, APP_NAME: {1}, APP_DESC: {2}",
                        item.APP_ID, item.APP_NAME, item.APP_DESC);
                }

    [Table(Name = "T_IC_APP")]
            public class T_IC_APP
            {
                [Column(IsPrimaryKey = true, Name = "APP_ID")]
                public string APP_ID { get; set; }

                [Column(Name = "APP_NAME")]
                public string APP_NAME { get; set; }

                [Column(Name = "APP_DESC")]
                public string APP_DESC { get; set; }
            }

    ------------------------------------------------------------------------

     Book[] books = new Book[]
                {
                    new Book("Ajax in Action", "Manning", 2005),
                    new Book("Windows Forms in Action", "Manning", 2006),
                    new Book("RSS and Atom in Action", "Manning", 2006)
                };

                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)
                        )

                );

                Console.WriteLine(xml);
            }

        class Book
        {
            public string Publisher;
            public string Title;
            public int Year;

            public Book(string title, string publisher, int year)
            {
                Title = title;
                Publisher = publisher;
                Year = year;
            }
        }

  • 相关阅读:
    MySQL 知识点
    用PHP操作http中Etag、lastModified和Expires标签
    Open Flash Chart在php中的使用教程
    Cmake,source_group
    Cmake调用NSIS(一个可执行文件,其实就是一个编译器)编译NSIS脚本问题研究
    VS2010安装与测试编译问题(fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt)
    Cmake find_package()相关
    Cmake,链接一个外部(也可能是第三方,也可能是自己编译的)库
    逆向工程入门指南
    Cmake的install与file命令的区别
  • 原文地址:https://www.cnblogs.com/RobotTech/p/1914014.html
Copyright © 2020-2023  润新知