问题标签 [linq-to-objects]

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 投票
3 回答
43505 浏览

c# - Linq to objects - 选择第一个对象

我对 linq 几乎一无所知。

我正在这样做:

这让我得到了符合该标准的所有正在运行的进程。

但我不知道如何获得第一个。我可以在网上找到的例子似乎暗示我必须这样做

这让我觉得有点难看,如果没有匹配的进程也会抛出异常。有没有更好的办法?

更新

我实际上是在尝试找到第一个匹配的项目,然后调用SetForegroundWindow

我想出了这个解决方案,它也让我觉得丑陋和可怕,但比上面的要好。有任何想法吗?

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 投票
6 回答
40776 浏览

.net - 根据属性值过滤 linq 列表

我有一个List<int>和一个List<customObject>。customObject 类有一个 ID 属性。如何获取List<customObject>仅包含 ID 属性在List<int>使用 LINQ 中的对象?

编辑:我接受了 Konrads 的回答,因为它更容易/更直观地阅读。

0 投票
22 回答
299754 浏览

c# - IEnumerable 上的动态 LINQ OrderBy/ 可查询的

我在动态 LINQ 的VS2008 示例中找到了一个示例,该示例允许您使用类似 SQL 的字符串(例如OrderBy("Name, Age DESC")),用于排序。不幸的是,包含的方法仅适用于IQueryable<T>. 有没有办法获得此功能IEnumerable<T>

0 投票
2 回答
26280 浏览

linq-to-objects - 列表到 IQueryable

我有一个列表,我想将它包装到一个 IQueryable 中。

这可能吗?

0 投票
3 回答
990 浏览

linq-to-objects - 可查询的对于性能优于 O(n) 的对象?

是否有任何用于 linq-to-objects 的 IQueriable 实现比调用 myEnumerable.AsQueriable() 时获得的默认 O(n) 线性搜索性能更好?

我看过http://www.codeplex.com/i4o/它有更好的性能,但似乎依赖于在 IndexedCollection 上使用扩展方法,而不是让 IndexedColleciton 实现 IQueriable。

我很想保持我的界面返回 IQueriable<T> 因为我不希望任何人知道他们是在访问缓存还是数据库。

0 投票
9 回答
43971 浏览

linq - 如何调试 LINQ 语句

我有一个 Linq to objects 语句

确认对象在 System.Linq.Enumerable.WhereListIterator`1.MoveNext() 处返回“Object Null or Not A Reference”

如果查询的结果为空,它只会返回一个空的枚举数。我知道语句中没有空对象。是否可以单步执行 LINQ 语句以查看它在哪里崩溃?

编辑当我说我知道没有空对象的事实时,事实证明我在撒谎:[,但问题仍然存在,尽管我假设答案是“你不能真的”

LINQPad 是个好主意,我用它来自学 LINQ,但我可能会再次将其视为调试/斜线和刻录风格工具

0 投票
3 回答
2943 浏览

linq - 具有多个聚合的 LINQ 查询

如何创建等效的 Linq To Objects 查询?

0 投票
3 回答
864 浏览

linq - 如何使用 LINQ 获取 IQueryable 中每一列的最大数据大小

我有一个采用 IQueryable 的方法。是否有一个 LINQ 查询可以返回 IQueryable 中每一列的大小?

更清楚地说:这是 Linq-to-objects。我想获取每个“列”的 ToString() 的长度。