问题标签 [linq]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
9 回答
21852 浏览

linq - Learning about LINQ

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

0 投票
4 回答
5530 浏览

c# - 如何在当前程序集中找到具有特定名称的 C# 接口的实现?

我有一个调用接口IStep,可以进行一些计算(参见“名词王国中的执行”)。在运行时,我想按类名选择适当的实现。

0 投票
2 回答
8346 浏览

.net - 如何使用 Linq 对通用集合进行分页?

我有一个 System.Generic.Collections.List(Of MyCustomClass) 类型的对象。

给定整数变量 pagesize 和 pagenumber,如何仅查询 MyCustomClass 对象的任何单页?

0 投票
6 回答
1808 浏览

c# - 我错过了一些关于 LINQ 的东西吗?

我似乎遗漏了一些关于 LINQ 的东西。对我来说,它看起来像是将一些我最不喜欢的 SQL 元素转移到 C# 语言中,并将它们用于其他事情。

我的意思是,我可以看到在数据库以外的事物上使用类似 SQL 的语句的好处。但是,如果我想编写 SQL,那么为什么不直接编写 SQL 并将其排除在 C# 之外呢?我在这里想念什么?

0 投票
1 回答
561 浏览

linq - 模拟和 IQueryable

我在尝试基于 NHibernate 测试以下 IRepository 时遇到了问题:

地狱如何以IQueryable<T>返回给定集合以交换某些表达式的方式模拟返回。我觉得我对IQueryable<T>……有些误解

0 投票
1 回答
1637 浏览

c# - IntelliSense for XElement objects with XML schema

Reading an article called "Increase LINQ Query Performance" in July's MSDN magazine, the author states that using an Imports in VB providing a path to schema in the current project will turn IntelliSense on for XElement. In the code provided, he uses statements like xelement.@name to retreive attributes values and so on.

I did not try this out myself in VB but I would like to use that in C#. This really looks like LINQ to XSD.

Is there any equivalent in C#? It seems that it is not possible to use a namespace inside C# code, there is no using equivalent to this Import statement.

0 投票
9 回答
34555 浏览

linq - NHibernate 与 LINQ to SQL

作为一个没有在实际项目中使用过这两种技术的人,我想知道是否有人知道这两种技术如何相互补充以及它们的功能有多少重叠?

0 投票
5 回答
11406 浏览

c# - GridView 控件中使用 LinqDataSource 和分页的总行数

我在获取使用 Paging 并使用 LinqDataSource 作为数据源的 Gridview 中显示的项目的总行数时遇到问题。

我尝试了几种方法:

每次返回 -1。

只给我当前页面的计数,而不是总数。

还有其他建议吗?

0 投票
11 回答
52653 浏览

c# - 获得 IEnumerable 计数的最佳方法

在不枚举集合中的所有项目的情况下获取 IEnumerable 集合中的项目计数的最佳/最简单方法是什么?

可以使用 LINQ 或 Lambda 吗?

0 投票
14 回答
13854 浏览

c# - 您使用过的最酷的 C# LINQ/Lambdas 技巧?

看到一篇关于 C# 中隐藏功能的帖子,但没有很多人写过 linq/lambdas 示例,所以......我想知道......

你见过/写过的 C# LINQ 和/或 Lambdas/匿名委托的最酷(最优雅的)用法是什么?

如果它也已投入生产,将获得奖励!