I have this query
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
var query =
from n in names
where n.Contains ("a") // Filter elements
orderby n.Length // Sort elements
select n.ToUpper(); // Translate each element (project)
foreach(var item in query)
Console.WriteLine(item);
works perfectly, gives the result JAY MARY HARRY as expected.
But when I run this
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
var query2 = names.Where(n=> n.Contains("a")).OrderBy(n=>n.Length).Select(n=>n.ToUpper);
foreach(var item in query2)
Console.WriteLine(item);
I get this message The type arguments for method 'System.Linq.Enumerable.Select<TSource,TResult>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
I tried running this too
var query2 = names.Where(n=> n.Contains("a")).OrderBy(n=>n.Length).Select<string, string>(n=>n.ToUpper);
It says Cannot convert method group 'ToUpper' to non-delegate type 'string'. Did you intend to invoke the method? Cannot convert lambda expression to delegate type 'System.Func<string,string>' because some of the return types in the block are not implicitly convertible to the delegate return type.
I don’t know what’s happening? Can any one tell me why the query syntax is working fine method syntax is not working?
You are missing a pair of parentheses after
ToUpper: