标签: extension-methods

Distinct()与lambda?

是的,所以我有一个可枚举的,并希望从中获得不同的值.

使用System.Linq,当然有一个名为的扩展方法Distinct.在简单的情况下,它可以在没有参数的情况下使用,例如:

var distinctValues = myStringList.Distinct();
Run Code Online (Sandbox Code Playgroud)

好的,但如果我有一个可以指定相等性的可枚举对象,唯一可用的重载是:

var distinctValues = myCustomerList.Distinct(someEqualityComparer);
Run Code Online (Sandbox Code Playgroud)

equality comparer参数必须是.的实例IEqualityComparer<T>.当然,我可以做到这一点,但它有点冗长,而且很有说服力.

我所期望的是一个需要lambda的重载,比如Func <T,T,bool>:

var distinctValues
    = myCustomerList.Distinct((c1, c2) => c1.CustomerId == c2.CustomerId);
Run Code Online (Sandbox Code Playgroud)

任何人都知道是否存在某些此类扩展或某些等效的解决方法?或者我错过了什么?

或者,有没有一种方法可以指定IEqualityComparer内联(embarass me)?

更新

我找到了Anders Hejlsberg对MSDN论坛中关于这个主题的帖子的回复.他说:

您将遇到的问题是,当两个对象比较相等时,它们必须具有相同的GetHashCode返回值(否则Distinct内部使用的哈希表将无法正常运行).我们使用IEqualityComparer,因为它将Equals和GetHashCode的兼容实现打包到一个接口中.

我认为那是有道理的..

c# lambda extension-methods c#-3.0

714
推荐指数
8
解决办法
37万
查看次数

C#有扩展属性吗?

C#有扩展属性吗?

例如,我可以添加一个扩展属性来DateTimeFormatInfo调用ShortDateLongTimeFormat哪个会返回ShortDatePattern + " " + LongTimePattern

c# extension-methods properties

712
推荐指数
5
解决办法
19万
查看次数

哪种方法表现更好:.Any()vs .Count()> 0?

System.Linq命名空间,我们现在可以扩展我们IEnumerable的有Any()Count() 扩展方法.

最近我被告知如果我想检查一个集合中是否包含一个或多个项目,我应该使用.Any()扩展方法而不是.Count() > 0扩展方法,因为.Count()扩展方法必须迭代所有项目.

其次,一些集合具有属性(未扩展方法),其是CountLength.使用它们会更好吗,而不是.Any().Count()

是啊/是?

.net linq performance extension-methods .net-3.5

555
推荐指数
6
解决办法
14万
查看次数

我可以将扩展方法添加到现有的静态类中吗?

我是C#中扩展方法的粉丝,但是没有成功将扩展方法添加到静态类,例如Console.

例如,如果我想向Console添加一个名为'WriteBlueLine'的扩展,那么我可以去:

Console.WriteBlueLine("This text is blue");
Run Code Online (Sandbox Code Playgroud)

我尝试通过添加一个本地的公共静态方法,将Console作为'this'参数...但没有骰子!

public static class Helpers {
    public static void WriteBlueLine(this Console c, string text)
    {
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine(text);
        Console.ResetColor();
    }
}
Run Code Online (Sandbox Code Playgroud)

这没有向Console添加'WriteBlueLine'方法......我做错了吗?或者要求不可能?

c# extension-methods static

519
推荐指数
11
解决办法
18万
查看次数

你最喜欢的C#扩展方法是什么?(codeplex.com/extensionoverflow)

让我们列出你发布优秀和最喜欢的扩展方法的答案.

要求是必须发布完整代码并提供示例和如何使用它的说明.

基于对该主题的高度关注,我在Codeplex上设置了一个名为extensionoverflow的开源项目.

请标记您的答案并接受将代码放入Codeplex项目中.

请发布完整的源代码而不是链接.

Codeplex新闻:

24.08.2010 Codeplex页面现在在这里:http://extensionoverflow.codeplex.com/

11.11.2008 XmlSerialize/XmlDeserialize现在已实现并且已经过单元测试.

11.11.2008仍有更多开发人员的空间.;-) 现在加入!

11.11.2008第三个贡献者加入了ExtensionOverflow,欢迎来到BKristensen

11.11.2008 FormatWith现在已实现且已经过单元测试.

09.11.2008第二个贡献者加入了ExtensionOverflow.欢迎来到chakrit.

09.11.2008我们需要更多开发人员.;-)

09.11.2008 ThrowIfArgumentIsNull现在在Codeplex上实现单元测试.

.net c# extension-methods open-source

478
推荐指数
57
解决办法
12万
查看次数

为什么IEnumerable上没有ForEach扩展方法?

受到另一个询问失踪问题的启发 Zip功能的:

为什么课堂上没有ForEach扩展方法Enumerable?还是在任 唯一获得ForEach方法的类是List<>.有没有理由错过(表演)?

.net c# vb.net extension-methods

368
推荐指数
10
解决办法
14万
查看次数

在C#中,当您在null对象上调用扩展方法时会发生什么?

是使用null值调用方法还是提供null引用异常?

MyObject myObject = null;
myObject.MyExtensionMethod(); // <-- is this a null reference exception?
Run Code Online (Sandbox Code Playgroud)

如果是这种情况,我永远不需要检查我的'this'参数是否为null?

c# parameters null extension-methods

310
推荐指数
5
解决办法
4万
查看次数

使用LINQ在一行代码中将string []转换为int []

我有一个字符串形式的整数数组:

var arr = new string[] { "1", "2", "3", "4" };
Run Code Online (Sandbox Code Playgroud)

我需要一个'真实'整数数组来推动它:

void Foo(int[] arr) { .. }
Run Code Online (Sandbox Code Playgroud)

我试图转换int,它当然失败了:

Foo(arr.Cast<int>.ToArray());
Run Code Online (Sandbox Code Playgroud)

我可以做下一个:

var list = new List<int>(arr.Length);
arr.ForEach(i => list.Add(Int32.Parse(i))); // maybe Convert.ToInt32() is better?
Foo(list.ToArray());
Run Code Online (Sandbox Code Playgroud)

要么

var list = new List<int>(arr.Length);
arr.ForEach(i =>
{
   int j;
   if (Int32.TryParse(i, out j)) // TryParse is faster, yeah
   {
      list.Add(j);
   }
 }
 Foo(list.ToArray());
Run Code Online (Sandbox Code Playgroud)

但两个看起来都很难看.

还有其他方法可以完成任务吗?

.net c# linq extension-methods

253
推荐指数
4
解决办法
21万
查看次数

必须在非泛型静态类中定义扩展方法

我收到错误:

必须在非泛型静态类中定义扩展方法

在线上:

public class LinqHelper
Run Code Online (Sandbox Code Playgroud)

这是基于Mark Gavells代码的助手类.我真的很困惑这个错误意味着什么,因为我确信它在周五离开时工作正常!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq.Expressions;
using System.Reflection;

/// <summary>
/// Helper methods for link
/// </summary>
public class LinqHelper
{
    public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
    {
        return ApplyOrder<T>(source, property, "OrderBy");
    }
    public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
    {
        return ApplyOrder<T>(source, property, "OrderByDescending");
    }
    public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
    {
        return ApplyOrder<T>(source, property, "ThenBy");
    }
    public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> …
Run Code Online (Sandbox Code Playgroud)

.net c# linq extension-methods compiler-errors

198
推荐指数
8
解决办法
28万
查看次数

代码相当于链式LINQ扩展方法调用中的'let'关键字

使用C#编译器查询理解功能,您可以编写如下代码:

var names = new string[] { "Dog", "Cat", "Giraffe", "Monkey", "Tortoise" };
var result =
    from animalName in names
    let nameLength = animalName.Length
    where nameLength > 3
    orderby nameLength
    select animalName; 
Run Code Online (Sandbox Code Playgroud)

在上面的查询表达式中,let关键字允许将值传递给where和orderby操作,而无需重复调用animalName.Length.

什么是LINQ扩展方法调用的等效集合,它实现了"let"关键字在这里的作用?

c# linq extension-methods linq-to-objects

186
推荐指数
3
解决办法
5万
查看次数