189

Overview

One of the things I've asked a lot about on this site is LINQ. The questions I've asked have been wide and varied and often don't have much context behind them. So in an attempt to consolidate the knowledge I've acquired on Linq I'm posting this question with a view to maintaining and updating it with additional information as I continue to learn about LINQ.

I also hope that it will prove to be a useful resource for other people wanting to learn about LINQ.

What is LINQ?

From MSDN:

The LINQ Project is a codename for a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. It extends C# and Visual Basic with native language syntax for queries and provides class libraries to take advantage of these capabilities.

What this means is that LINQ provides a standard way to query a variety of datasources using a common syntax.

What flavours of LINQ are there?

Currently there are a few different LINQ providers provided by Microsoft:

There are quite a few others, many of which are listed here.

What are the benefits?

  • Standardized way to query multiple datasources
  • Compile time safety of queries
  • Optimized way to perform set based operations on in memory objects
  • Ability to debug queries

So what can I do with LINQ?

Chook provides a way to output CSV files
Jeff shows how to remove duplicates from an array
Bob gets a distinct ordered list from a datatable
Marxidad shows how to sort an array
Dana gets help implementing a Quick Sort Using Linq

Where to start?

A summary of links from GateKiller's question are below:
Scott Guthrie provides an intro to Linq on his blog
An overview of LINQ on MSDN

ChrisAnnODell suggests checking out:

What do I need to use LINQ?

Linq is currently available in VB.Net 9.0 and C# 3.0 so you'll need Visual Studio 2008 or greater to get the full benefits. (You could always write your code in notepad and compile using MSBuild)

There is also a tool called LinqBridge which will allow you to run Linq like queries in C# 2.0.

Tips and tricks using LINQ

This question has some tricky ways to use LINQ

4

9 回答 9

13

LINQ 到实体:

在 Delicious.com 上标记了更多。

于 2008-10-12T11:00:03.143 回答
7

提及 LINQ to Entities,因为 ADO.NET 实体框架将是一个重要的 .NET 模块。

于 2008-08-28T22:52:29.147 回答
5

一些 LINQ 提示:

  • 在联接之前应用过滤器以提高查询性能
  • 使用对象引用比较过滤 LINQ 查询
  • 将聚合应用于 LINQ to SQL 查询中的空集合
  • 延迟加载 LINQ to SQL 中的属性
  • 使用打开急切加载的表值函数
  • 在 LINQ to Objects 查询中以正确的顺序放置连接
  • 在循环内编写 LINQ 查询

http://www.aspnetpro.com/articles/2009/04/asp200904zh_f/asp200904zh_f.asp

于 2009-04-03T20:14:24.807 回答
4

获取Linq in Action一书,这是一本易于阅读的编码书籍,它真正教您如何使用 Linq 和 .NET 3.5 的新功能,以及他们为该语言添加的一些很酷的部分。

于 2008-08-30T02:02:43.477 回答
4

恕我直言,一个被忽视但重要的好处是 LINQ 的编码效率,例如用这么少的代码可以完成多少工作。我个人发现查询语法易​​于阅读和理解。

于 2009-12-10T21:22:22.023 回答
3

关于使用LINQ to SQL 的一些注意事项:

微软真的杀死了 LINQ to SQL 吗?

LINQ to SQL 是 DOA 吗?

关于Entity Framework的第一个版本也存在一些争议,包括请愿书

于 2008-11-07T14:46:42.010 回答
3

我想,“有哪些 LINQ 风格?”的答案。不完整。首先,您可以创建自己的“风味”。是的,这是一项高级任务,但现在有很多不同的 LINQ 实现。

以下是 Charlie Calvert 博客上现有 LINQ 提供程序的列表(以及更多有关学习 LINQ 的资源):Links to LINQ

还有一个由 Matt Warren 撰写的关于如何创建自己的 LINQ 提供程序的优秀博客文章系列:LINQ: Building an IQueryable provider series

于 2009-11-06T17:56:08.483 回答
2

我的 2 美分,阅读“C# in Depth”一书中的“11 Query expressions and LINQ to Objects”和“12 LINQ beyond collections”章节,了解 LINQ 的工作原理。

于 2015-10-07T11:19:26.963 回答
0

对于 Linq 练习

如果您想通过练习和答案来练习 LINQ,那么设置起来非常容易,而且在我看来,非常棒:

https://github.com/walkhard/linq-exercises

从 git 下载,在 Visual Studio 中打开。你的工作是让测试通过。

[披露:我从中学到了一些 linq,并为该项目做出了贡献,所以是的,我认为这是一种很棒、快速和有效的学习方式。]

于 2017-01-28T00:34:07.033 回答