文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>深入理解linq的参考例子

深入理解linq的参考例子

时间:2010-11-16  来源:2012

http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

很全面的官方站点。

 

基本概念清楚后,可以参考如下的例子以加深理解

Linq in Action的第5章对于常用模式,性能有个综合的例子,很不错:

 

以下是一些常见的使用场景举例  

    IEnumerable<String> titles = SampleData.Books.Select(book => book.Title);

    String[] array = titles.ToArray();

    List<String> list = titles.ToList();

    Dictionary<String, Book> isbnRef = SampleData.Books.ToDictionary(book => book.Isbn);

    Book linqRules = isbnRef["0-111-77777-2"];

    Dictionary<int, string> frenchNumbers;

    frenchNumbers = new Dictionary<int, string>();

    frenchNumbers.Add(0, "zero");

    frenchNumbers.Add(1, "un");

    frenchNumbers.Add(2, "deux");

    var evenFrenchNumbers = from entry in frenchNumbers where (entry.Key % 2) == 0 select entry.Value;

    List<Book> books = new List<Book>() {

      new Book { Title="LINQ in Action" },

      new Book { Title="LINQ for Fun" },

      new Book { Title="Extreme LINQ" } };

    var titles = books.Where(book => book.Title.Contains("Action")).Select(book => book.Title);

    var books =SampleData.Books.Select((book, index) => new { index, book.Title })

        .OrderBy(book => book.Title);

    var count = "Non-letter characters in this string: 8".Where(c => !Char.IsLetter(c)).Count();

    Object[] array = { "String", 12, true, 'a' };

    var types = array.Select(item => item.GetType().Name)

        .OrderBy(type => type);

      var query = from book in SampleData.Books

        where book.Title.Length > 10

        orderby book.Price

        select new { Book=book.Title, book.Price };

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载