私はこのクエリを持っています
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);
完全に機能し、JAY MARY HARRY
期待どおりの結果が得られます。
しかし、私がこれを実行すると
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);
このメッセージが表示されます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.
私もこれを実行してみました
var query2 = names.Where(n=> n.Contains("a")).OrderBy(n=>n.Length).Select<string, string>(n=>n.ToUpper);
それは言う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.
何が起こっているのか分かりませんか?クエリ構文が正常に機能している理由を誰かに教えてもらえますか?メソッド構文が機能していません。