How can I extend DynamicQuery.cs to implement a .Single method?
        Posted  
        
            by Yoenhofen
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Yoenhofen
        
        
        
        Published on 2010-03-28T00:18:49Z
        Indexed on 
            2010/03/28
            0:23 UTC
        
        
        Read the original article
        Hit count: 907
        
I need to write some dynamic queries for a project I'm working on. I'm finding out that a significant amount of time is being spent by my program on the Count and First methods, so I started to change to .Single, only to find out that there is no such method.
The code below was my first attempt at creating one (mostly copied from the Where method), but it's not working. Help?
 public static object Single(this IQueryable source, string predicate, params object[] values)
    {
        if (source == null) throw new ArgumentNullException("source");
        if (predicate == null) throw new ArgumentNullException("predicate");
        LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, typeof(bool), predicate, values);
        return source.Provider.CreateQuery(
            Expression.Call(
                typeof(Queryable), "Single",
                new Type[] { source.ElementType },
                source.Expression, Expression.Quote(lambda)));
    }
© Stack Overflow or respective owner