extension-methods

Why is the query operator 'ElementAt' is not supported in LINQ to SQL?

馋奶兔 提交于 2019-12-03 16:58:23
问题 In LINQ to SQL, I get the exception " The query operator 'ElementAt' is not supported. " When trying to use the ElementAt extension method on an IQueryable returned from a LINQ to SQL query. Here is the stack trace: at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc) at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) at System.Data.Linq

Create IEnumerable<T>.Find()

亡梦爱人 提交于 2019-12-03 16:17:11
问题 I'd like to write: IEnumerable<Car> cars; cars.Find(car => car.Color == "Blue") Can I accomplish this with extension methods? The following fails because it recursively calls itself rather than calling IList.Find(). public static T Find<T>(this IEnumerable<T> list, Predicate<PermitSummary> match) { return list.ToList().Find(match); } Thanks! 回答1: This method already exists. It's called FirstOrDefault cars.FirstOrDefault(car => car.Color == "Blue"); If you were to implement it yourself it

ICollection / ICollection<T> ambiguity problem

拈花ヽ惹草 提交于 2019-12-03 16:12:55
Just want to make simple extension for syntactic sygar : public static bool IsNotEmpty(this ICollection obj) { return ((obj != null) && (obj.Count > 0)); } public static bool IsNotEmpty<T>(this ICollection<T> obj) { return ((obj != null) && (obj.Count > 0)); } It works perfectly when I work with some collections, but when working with others I get The call is ambiguous between the following methods or properties: 'PowerOn.ExtensionsBasic.IsNotEmpty(System.Collections.IList)' and 'PowerOn.ExtensionsBasic.IsNotEmpty(System.Collections.Generic.ICollection)' Is there any canonical solution to this

Lambdas within Extension methods: Possible memory leak?

怎甘沉沦 提交于 2019-12-03 14:11:40
I just gave an answer to a quite simple question by using an extension method. But after writing it down i remembered that you can't unsubscribe a lambda from an event handler. So far no big problem. But how does all this behave within an extension method?? Below is my code snipped again. So can anyone enlighten me, if this will lead to myriads of timers hanging around in memory if you call this extension method multiple times? I would say no, cause the scope of the timer is limited within this function. So after leaving it no one else has a reference to this object. I'm just a little unsure,

Why List(Of T) doesn't have Count()?

杀马特。学长 韩版系。学妹 提交于 2019-12-03 13:07:55
Public Class MyList Inherits List(Of MyObject) Public ReadOnly Property SelectedCount() As Integer Get Return Me.Count(Function(obj) obj.IsSelected) End Get End Property End Class The above code causes a compile-time error. As you can see, I'm trying to use extension method Count(<predicate>) . I guess the error is because there is a similarly-named property Count in List class itself too, hiding the extension member. My questions here are: Do I need to explicitly cast my class to something else to access Count extension method? If so, exactly which class should it be in the above scenario?

With the advent of extension methods, are abstract classes less attractive?

南笙酒味 提交于 2019-12-03 13:03:00
One interesting aspect of extension methods in .NET is the fact that you can apply them to interfaces. For me, it seems nice that I can define functionality near the interface without defining an abstract class that clutters the assembly. I know that abstract classes are not obsolete or anything, but how do you feel about utilizing this side effect in your code? Example: public static class IUserExtensions { public static bool IsCurrentUser(this IUser user) { return (HttpContext.Current.User != null && HttpContext.Current.User.Identity.Name == user.ID.ToString()); } } public interface IUser {

Linq extension method, how to find child in collection recursive

对着背影说爱祢 提交于 2019-12-03 12:27:25
I'm already familiar with Linq but have little understanding of extension methods I'm hoping someone can help me out. So I have this hierarchical collection pseudo code ie: class Product prop name prop type prop id prop List<Product> children And I have a list of products List products. Is there any way I can look for product in this collection by the id with a extension method ? In other words I need one item somewhere within the hierarchy. Here is a generic solution that will short-circuit traversal of the hierarchy once a match is found. public static class MyExtensions { public static T

C# Extension Methods only visible and accessible within one class (“private”)

那年仲夏 提交于 2019-12-03 12:11:06
问题 Is it possible, in C#, to create extension methods on a class but restrict visibility/accessibility within a class? (e.g. Extension Method A on class M is only accessible within class Z) Example: class A { String foo = ""; String bar = foo.MakeMillionaire("arg"); } In above example I want the extension method "MakeMillionaire" extending the String class only to be visible and accessible within class A. Can I do this somehow by defining the extension method in a static class within class A?

How can I call C# extension methods in VB code

北城以北 提交于 2019-12-03 12:08:35
I have a class library with some extension methods written in C# and an old website written in VB. I want to call my extension methods from the VB code but they don't appear in intelisense and I get compile errors when I visit the site. I have got all the required Import s because other classes contained in the same namespaces are appearing fine in Intelisense. Any suggestions EDIT: More info to help with some comments. my implementation looks like this //C# code compiled as DLL namespace x.y { public static class z { public static string q (this string s){ return s + " " + s; } } } and my

How to call an extension method from own class without casting?

坚强是说给别人听的谎言 提交于 2019-12-03 11:54:10
I'm trying to call an extension method on my own class, but it fails to compile. Consider the following lines of code: public interface IHelloWorld { } public static class Extensions { public static string HelloWorld(this IHelloWorld ext) { return "Hello world!"; } } public class Test : IHelloWorld { public string SaySomething() { return HelloWorld(); } } Basically I'm extending on the interface. I keep getting this error: The name 'HelloWorld' does not exist in the current context Can anybody explains this to me? When I do a cast all seems well: return ((Test)this).HelloWorld(); Any