zoukankan      html  css  js  c++  java
  • 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;
            }
        }

  • 相关阅读:
    26、实例化需求:团队如何交付正确的软件
    25、华胥引
    24、老子
    23、禅与摩托车维修艺术(又名万里任禅游)
    22、中国哲学简史
    21、人类简史-从动物到上帝(赫拉利)
    20、淘宝技术这十年
    19.验收测试驱动开发
    18. Scrum敏捷软件开发
    17、胡适谈哲学与人生
  • 原文地址:https://www.cnblogs.com/RobotTech/p/1914014.html
Copyright © 2011-2022 走看看