extension-methods

How can I refactor this IQueryable<T> Repository Method?

Hi Guys, I'm working on a .NET 4 application, C#, Entity Framework 4, SQL Server 2008. I have a 7 tables in my database, each representing a specific level of location (Country, State, City, Neighborhood, etc). Now in my Repository I am trying to define an interface contract which has only one Find() method. To do this, I've created ...

What would be happen if all static methods would be extension method default ?

Hello everybody Static methods always should to encapsulate arguments by parenthesis . So it is much more easy to use extension methods while typing. That is one of the reason why i like extension methods. I am not sure when time it is better to use extension or static methods. And i am thinking that what would be happen if all static ...

Is it possible to write a extension method for an abstract class.

Why I'm unable to extend an abstract class. Is there any work around to achieve this? In silverlight, Enum.GetNames is missing. So, I would like to extend it and have it in my utility assembly. By then, got into this. ...

What's your favorite LINQ to Objects operator which is not built-in?

With extension methods, we can write handy LINQ operators which solve generic problems. I want to hear which methods or overloads you are missing in the System.Linq namespace and how you implemented them. Clean and elegant implementations, maybe using existing methods, are preferred. ...

ASP.NET (VB) Extension Method not working as expected.

I have the following Extension Method Imports System.Runtime.CompilerServices Namespace Extensions Public Module IntegerExtensions <Extension()> Public Function ToCommaDeliminatedNumber(ByVal int As Integer) As String Dim _input As String = int.ToString Select Case int Case I...

How to make [example] extension method more generic/functional/efficient?

I needed a double[] split into groups of x elements by stride y returning a List. Pretty basic...a loop and/or some linq and your all set. However, I have not been spending much time on extension methods and this looked like a good candidate for some practice. The naive version returns what I am looking for in my current application.... ...

Extension-methods VS static method resolution - what do I miss in spec?

Hi! I've added an extension method that is a shortcut to string.Format: public static string Format(this string format, params object[] args) { return String.Format(format, args); } When I invoke this method like this: "{0}".Format(1); everything works like a charm. While "{0}".Format("1"); does not compile with this error...

Static extension methods supporting member constraints

I need to implement a static extension method supporting member constraints on some basic primitive types like integers, floats, etc. Here's my code for signed integers: module MyOperators = let inline foo (x : ^T) = (^T : (static member Foo : ^T -> int) (x)) type System.Int32 with static member Foo(x : Int32) = 7 // ...

Check if all items in a Collection have the same value.

an extension method on a collection named MeasurementCollection checks if the property Template.Frequency (Enum) of each item has the same value. public static bool IsQuantized(this MeasurementCollection items) { return (from i in items select i.Template.Frequency) .Distinct() ...

Pivoting a collection of arrays

Basically I have a collection of objects each implement a member of Type IValueCollection public interface IValueCollection : IEnumerable<decimal> { decimal this[int index] { get; set; } } MeasurementCollection.Values is of type IValueCollection. With the logic below I want to pivot a collection of IValueCollection and wrote the...

How to select an element in a List of N using Where extension function?

Hello, Suppose I have a class AddressType defined as is: public class AddressType { public int AddressTypeId { get; set; } public string Description { get; set; } } Having a List object in code, how do I select an AddressType object with a known AddressTypeId property? I have never used the List.Where extension function.......

How FirstOrDefault extension method works?

I was wondering on how FirstOrDefault extension method works? Which one of the following algorithms does it follows? Use: var arr = new[] {1, 2, 3, 4, 5, 6, 7}; return arr.FirstOrDefault(x => x%2 == 0); Algorithm 1: for(int i = 0; i < arr.Length; i++) { if(arr[i] % 2 == 0) return arr[i]; } return 0; Algorithm 2: var list ...

Extension method and type constraints

I am starting to play with extension methods and i came across with this problem: In the next scenario i get a: "extension method has a type constraint that can never be satisfied" Public Interface IKeyedObject(Of TKey As IEquatable(Of TKey)) ReadOnly Property InstanceKey() As TKey End Interface <Extension()> _ Public Function To...

IQueryable Extension: create lambda expression for querying a column for a keyword

Hi, I started with the IQueryable extension methods from this example on CodePlex. What i believe i need is an IQueryable extension method to "Where", where the method signature looks like: public static IQueryable<T> Where<T>(this IQueryable<T> source, string columnName, string keyword) and effectively does this (assuming T.columnNa...

Combined "Check Add or Fetch" from Dictionary

I'm tired of this dictionary idiom: Dictionary<Guid,Contact> Contacts; //... if (!Contacts.ContainsKey(id)) { contact = new Contact(); Contacts[id] = contact; } else { contact = Contacts[id]; } It would be nice if there was a syntax tha...

Invoking interface extension methods from implementor is weird in C#

Invoking an extension method that works on a interface from an implementor seems to require the use of the this keyword. This seems odd. Does anyone know why? Is there an easier way to get shared implementation for an interface? This irks me as I'm suffering multiple inheritance/mixin withdrawl. Toy example: public interface ITest ...

Extension methods in referenced assemblies?

If I try to call my extension method which is defined like this: Module LinqExtensions <System.Runtime.CompilerServices.Extension()> _ Public Function ToSortableBindingList(Of TSource)(ByVal source As IEnumerable(Of TSource)) As IBindingList If (source Is Nothing) Then Throw New ArgumentNullException("source") End If ...

extension method call another in same extension class - good design?

Hi, i ask myself if it is a good design if an extension method uses another in the same extension class. public class ClassExtensions { public static bool IsNotNull<T>(this T source) where T : class { return !source.IsNull(); } public static bool IsNull<T>(this T source) where T : class { retu...

extension methods from C# dll doesn't work as extensions in VB.NET

actually I don't know whether they should work I made a library in C# and I've been told by people that one of mine methods don't work in VB.NET as extension http://valueinjecter.codeplex.com/Thread/View.aspx?ThreadId=227498 this is the method: public static PropertyDescriptorCollection GetProps(this object o) { return GetProps(o.G...

Can F# modules be monkey-patched?

Quick question. I just read that if you wanted to add a function to e.g. the List module, you can define a new List module with that function: module List let foo = // ... Does this have the effect of adding foo to the main List module, or do you have to explicitly open the new List? The former seems like Ruby's "monkey patching"; I...