views:

137

answers:

1

(sorry, I tried to format it well, but I cannot get the code formatting to work correctly)

I get:

Incorrect number of arguments supplied for call to method
'System.Linq.IQueryable`1[System.String]
Take[String](System.Linq.IQueryable`1[System.String], Int32)'

when I execute:

string[] companies = { "Consolidated Messenger", "Alpine Ski House", "Southridge Video", "City Power & Light",
       "Coho Winery", "Wide World Importers", "Graphic Design Institute", "Adventure Works",
       "Humongous Insurance", "Woodgrove Bank", "Margie's Travel", "Northwind Traders",
       "Blue Yonder Airlines", "Trey Research", "The Phone Company",
       "Wingtip Toys", "Lucerne Publishing", "Fourth Coffee" };

// The IQueryable data to query.
IQueryable<String> queryableData = companies.AsQueryable<string>();

// EXCEPTION HERE
Expression e2 = Expression.Call(
    typeof(Queryable).GetMethods().Where(m => m.Name == "Take")
     .Single().MakeGenericMethod(new Type[] { typeof(string) }),
    new Expression[] { Expression.Constant(4) });

IQueryable<string> res = queryableData.Provider.CreateQuery<string>(e2);

foreach (string s in res)
{
    Console.WriteLine(s);
}

I think that I need to pass in the queryable object itself, but I cannot figure out how to do this (if it is even required).

Any help is appreciated.

Thanks.

+2  A: 
Expression e2 = Expression.Call(
    typeof(Queryable).GetMethods().Where(m => m.Name == "Take")
        .Single().MakeGenericMethod(new Type[] { typeof(string) }),
    new Expression[] {
        Expression.Constant(queryableData),
        Expression.Constant(4) });
Pavel Minaev
Nice. Plus you can simplify the statement by replacing the "Where" call with the "Single" call and replacing new Type[] { typeof(string) } with typeof(string), but that's just quibbling.
Michael La Voie
Thank you, exactly what I was looking for.
Sako73