Tag: linq

相交LINQ查询

如果我有一个IEAumerable,其中ClassA公开一个types为long的ID属性。 是否有可能使用Linq查询来获取属于第二个IEnumerable的ID的所有ClassA实例? 换句话说,这可以做到吗? IEnumerable<ClassA> = original.Intersect(idsToFind….)? 其中original是一个IEnumerable<ClassA> ,idsToFind是IEnumerable<long> 。

扩展LINQ接受可枚举枚举

在使用Linq扩展时,看到这样的代码是正常的: IEnumerable<int> enumerable = GetEnumerable(); int sum = 0; if (enumerable != null) { sum = enumerable.Sum(); } 为了提高代码质量,我编写了下面的扩展方法来检查可空的枚举types并打断linq的执行。 public static IEnumerable<T> IgnoreIfEmpty<T>(this IEnumerable<T> enumerable) { if (enumerable == null) yield break; foreach (var item in enumerable) { yield return item; } } 所以,我可以重构代码是这样的: var sum = GetEnumerable().IgnoreIfEmpty().Sum(); 我现在的问题是: 我的扩展方法在运行时会受到哪些处罚? 以这种方式扩展linq是一个好习惯吗? 更新:我的目标框架是:3.5

基本的LINQexpression式为一个ItemCollection

我有一个我想用LINQ查询的ItemCollection 。 我尝试了以下(做作)的例子: var lItem = from item in lListBox.Items where String.Compare(item.ToString(), "abc") == true select item; Visual Studio不断告诉我Cannot find an implementation of the query pattern for source type 'System.Windows.Controls.ItemCollection'. 'Where' not found. Consider explicitly specifying the type of the range variable 'item'. Cannot find an implementation of the query pattern for source type 'System.Windows.Controls.ItemCollection'. […]

计算IEnumerable的计数(非通用)

任何人都可以帮助我IEnumerable Count扩展方法(非通用接口)。 我知道这不是在LINQ支持,但如何手动写入?

IEnumerable是空的?

我知道这可能不重要/影响性能的大部分,但我讨厌获得IEnumerable和做.Count()的想法。 是否有一个IsEmpty或NotEmpty或某些function? (类似于stl empty())

使用Linq返回逗号分隔的string

我在申请中有一堂课 public class ProductInfo { public int ProductId {get;set;} public int ProductType{get;set;} } 我想写一个linq查询,它可以返回一个ProductId列表,以逗号分隔的格式,其中ProductType等于某个数字? 我试着用我的Linq语句使用string.join,但它似乎没有工作。

LINQ最大()与空值

我有一个列表,其中包含一堆点(与X和Y组件)。 我想获得列表中所有点的最大X,如下所示: double max = pointList.Max(p=> pX); 问题是当我在列表中有一个空而不是一个点。 解决这个问题最好的办法是什么?

如何在多个字段中使用LINQ Distinct()

我有从数据库派生的以下EF类 (简化) class Product { public string ProductId; public string ProductName; public string CategoryId; public string CategoryName; } ProductId是表格的主键 。 对于由数据库devise人员做出的糟糕的devise决定(我无法修改它),我在这个表中有CategoryId和CategoryName 。 我需要一个DropDownList (独特) CategoryId作为值和CategoryName作为文本 。 所以我申请了下面的代码: product.Select(m => new {m.CategoryId, m.CategoryName}).Distinct(); 这在逻辑上应该创build一个具有CategoryId和CategoryName属性的匿名对象。 Distinct()保证没有重复对( CategoryId , CategoryName )。 但实际上这是行不通的。 就我所了解的Distinct()作品,只有一个字段在集合中,否则它只是忽略它们…是否正确? 有没有解决办法? 谢谢! UPDATE 对不起, product是: List<Product> product = new List<Product>(); 我发现了一个替代方法来获得与Distinct()相同的结果: product.GroupBy(d => new {d.CategoryId, […]

用于假人的expression树?

我是这种情况下的假人。 我试图在谷歌上看到这些是什么,但我只是不明白。 有人能给我一个简单的解释,说明他们是什么,为什么他们有用吗? 编辑:我正在谈论.NET中的LINQfunction。

C#列表parsing简介

我如何在C#中执行列表推导?