zoukankan      html  css  js  c++  java
  • LINQ notes1 intro

    Apress.Pro.LINQ.Language.Integrated.Query.in.C#2008

    Array:

    string[] greetings = { "hello world", "hello LINQ", "hello Apress" };
    var items = from s in greetings
                where s.EndsWith("LINQ")
                select s;
    foreach (var item in items)
        Console.WriteLine(item);

    XML:

    XElement books = XElement.Parse(
        @"<books>
            <book>
                <title>Pro LINQ: Language Integrated Query in C# 2008</title>
                <author>Joe Rattz</author>
            </book>
            <book>
                <title>Pro WF: Windows Workflow in .NET 3.0</title>
                <author>Bruce Bukovics</author>
            </book>
            <book>
                <title>Pro C# 2005 and the .NET 2.0 Platform, Third Edition</title>
                <author>Andrew Troelsen</author>
            </book>
        </books>");
    var titles = from book in books.Elements("book")
                 where (string)book.Element("author") == "Joe Rattz"
                 select book.Element("title");
    foreach (var title in titles)
        Console.WriteLine(title.Value);

    Database:

    Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind");
    var custs = from c in db.Customers
                where c.City == "Rio de Janeiro"
                select c;
    foreach (var cust in custs)
        Console.WriteLine("{0}", cust.CompanyName);

    以上列出对于不同对象的基本操作。

    还有其它比如LINQ to Objects,LINQ to XML,LINQ to DataSet,LINQ to SQL。甚至注册表和Excel文件。

    另外LINQ不仅仅是作为查询工具

    string[] numbers = { "0042", "010", "9", "27" };
    int[] nums = numbers.Select(s => Int32.Parse(s)).ToArray();
    foreach(int num in nums)
    Console.WriteLine(num);

    这样,可以做到类型的转换。

  • 相关阅读:
    css 设置特定宽度,超出部分用...代替
    php 二维数组根据某个key去重
    一些大厂开源项目
    JavaScript如何解析本地xml文件
    console的知识点
    toLocalDateString的用途
    在Vue中使用Object.freeze
    淘宝npm镜像
    JavaScript Async/Await
    vue中的addEventListener和removeEventListener
  • 原文地址:https://www.cnblogs.com/cheetahw/p/2628651.html
Copyright © 2011-2022 走看看