3

(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.

1
  • Thanks for the formatting help. Commented Dec 11, 2009 at 19:12

1 Answer 1

3
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) });
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.