(對不起,我想很好地格式化,但我不能讓代碼格式化才能正常工作)構建一個LINQ表達式
我得到:
Incorrect number of arguments supplied for call to method
'System.Linq.IQueryable`1[System.String]
Take[String](System.Linq.IQueryable`1[System.String], Int32)'
當我執行:
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);
}
我認爲我需要在可查詢的對象本身來傳遞,但我無法弄清楚如何做到這一點(如果它甚至需要)。
任何幫助表示讚賞。
謝謝。
感謝您的格式幫助。 – Sako73 2009-12-11 19:12:41