C # Linq Select problem in method chain

Note that _src inherit IQueryable<U>and V inherit new();

I wrote the following statement, there is no syntax error.

IQueryable<V> a = from s in _src where (s.Right - 1 == s.Left) select new V();

But if I RE-wrote it as follows, the Visual Studio editor complains about the error in the "Select"

IQueryable<V> d = _src.Where(s => s.Right - 1 == s.Left).Select(s=> new V());

Error:

The type arguments cannot be inferred from the usage. Try specifying the type arguments explicitly.
Candidates are:
  System.Collections.Generic.IEnumerable<V> Select<U,V>(this System.Collections.Generic.IEnumerable<U>, System.Func<U,V>) (in class Enumerable)
  System.Linq.IQueryable<V> Select<U,V>(this System.Linq.IQueryable<U>, System.Linq.Expressions.Expression<System.Func<U,V>>) (in class Queryable)

Can someone explain this phenomenon and what solution should fix the error?

=== Edit (2010-03-16 17:35) ===

Thanks to Mike Two. I also tried a simple example like you. It works, but this does not happen in mine. I sent the code as follows:

public class NSM<U, V> where U : IQueryable<U> where V : new()  
  {
    private U _src;
    public NSM(U source) { _src = source; }
    public IQueryable<V> LeafNodes
    {
      get
        {
          return from s in _src where (s.Right - 1 == s.Left) select new V();
        }
    }
  }

I want the LeafNodes function to be rewritten in the linq method method method. Any idea?

+5
source share
4 answers

_src? IQueryable? , , .

IQueryable< int > ints = Enumerable.Range( 4, 12 ).AsQueryable();

IQueryable< decimal > foo = from s in ints where s > 7 select s * 4.2m;

IQueryable< decimal > bar = ints.Where( s => s > 7 ).Select( s => s * 4.2m );

. , , ints ( _src) IQueryable, . - ? , .

EDIT: , .

, Queryable.Select Expression<Func<X, V>> Enumerable.Select Func<X,V>. Expression Select

public interface ILeftRight
{
    int Right { get;}
    int Left { get; }
}

public class NSM<X, U, V> where U : IQueryable<X> where X : ILeftRight where V : new()  
{
    private readonly U _src;
    public NSM(U source) { _src = source; }
    public IQueryable<V> LeafNodes
    {
        get
        {
            //return from s in _src where (s.Right - 1 == s.Left) select new V();
            Expression< Func< X, V > > expression = s => new V();
            return _src.Where( s => s.Right - 1 == s.Left ).Select( expression );
        }
    }
}

Expression<Func<X,V>> expression = s => new V();
IQueryable<V> d = _src.Where(s => s.Right - 1 == s.Left).Select(expression);
+1

- , , : Select IEnumerable<T> Select IQueryable<T>

+1

, , , Select, using, . , . , , - , .

+1

, AsEnumerable()

src.AsEnumerable().Select(s => new V());

IQueryable

IQueryable<V> x = src.AsEnumerable().Select(s => new V()).AsQueryable();
0

All Articles