Resolving extension methods/LINQ ambiguity

This is probably one of those rare cases where it makes sense to use an extern alias. In the properties page for the reference to System.Core (i.e. under References, select System.Core, right-click and select “Properties”), change the “Aliases” value to “global,SystemCore” (or just “SystemCore” if it’s blank to start with). Then in your code, write: … Read more

FindAll vs Where extension-method

Well, FindAll copies the matching elements to a new list, whereas Where just returns a lazily evaluated sequence – no copying is required. I’d therefore expect Where to be slightly faster than FindAll even when the resulting sequence is fully evaluated – and of course the lazy evaluation strategy of Where means that if you … Read more

Is it possible to implement mixins in C#?

It really depends on what you mean by “mixin” – everyone seems to have a slightly different idea. The kind of mixin I’d like to see (but which isn’t available in C#) is making implementation-through-composition simple: public class Mixin : ISomeInterface { private SomeImplementation impl implements ISomeInterface; public void OneMethod() { // Specialise just this … Read more

How to use Active Support core extensions

Since using Rails should handle this automatically I’m going to assume you’re trying to add Active Support to a non-Rails script. Read “How to Load Core Extensions”. Active Support’s methods got broken into smaller groups in Rails 3, so we don’t end up loading a lot of unneeded stuff with a simple require ‘activesupport’. Now … Read more

Using extension methods in .NET 2.0?

Like so: // you need this once (only), and it must be in this namespace namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)] public sealed class ExtensionAttribute : Attribute {} } // you can have as many of these as you like, in any namespaces public static class MyExtensionMethods { public static int MeasureDisplayStringWidth ( … Read more

The operation cannot be completed because the DbContext has been disposed error

This question & answer lead me to believe that IQueryable require an active context for its operation. That means you should try this instead: try { IQueryable<User> users; using (var dataContext = new dataContext()) { users = dataContext.Users.Where(x => x.AccountID == accountId && x.IsAdmin == false); if(users.Any() == false) { return null; } else { … Read more