tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

“Local” Queries 2nd edition

22 Feb 2009 1 mins Entity Framework, LINQ

Danny Simmons posted small helper method for runnning queries against local cache (in ObjectContext). Many of you are probably using similar method. But I don’t like to have to specify entity set as a string. That’s a first step for refactoring problems. 😃

Thus I created little bit different version, using same trick as Matthieu Mezil with Include.

public static IEnumerable<TEntity> Local<TEntity, TObjectContext>(this ObjectContext context, Expression<Func<TObjectContext, ObjectQuery<TEntity>>> entitySet) where TEntity : class
{
    if (!(entitySet.Body is MemberExpression))
        throw new ArgumentException("entitySet");
    string name = ((MemberExpression)entitySet.Body).Member.Name;
    return context.Local<TEntity>(name);
}

It’s using Danny’s original one to do the dirty work 😉, so it’s just a small helper for helper. You can call it like e.Local<DETAIL, MyEntities>(x => x.Details).ToArray(); instead of e.Local<DETAIL>("Details").ToArray();.

Profile Picture Jiří Činčura is .NET, C# and Firebird expert. He focuses on data and business layers, language constructs, parallelism, databases and performance. For almost two decades he contributes to open-source, i.e. FirebirdClient. He works as a senior software engineer for Microsoft. Frequent speaker and blogger at www.tabsoverspaces.com.