Linqでコンパイルされたクエリを理解し始めたところですが、奇妙な動作に出くわしました。
このクエリは正常にコンパイルされます。
public static Func<DataContext, string, object> GetJourneyByUrl =
CompiledQuery.Compile<DataContext, string, object>((DataContext dc, string urlSlug) =>
from j in dc.Journeys
where !j.Deleted
where j.URLSlug.Equals(urlSlug)
select new KeyValuePair<string, int>(j.URLSlug, j.JourneyId)
);
しかし、リターンタイプをオブジェクトからKeyValuePairに次のように変更しようとすると、次のようになります。
public static Func<DataContext, string, KeyValuePair<string, int>> GetJourneyByUrl =
CompiledQuery.Compile<DataContext, string, KeyValuePair<string, int>>((DataContext dc, string urlSlug) =>
from j in dc.Journeys
where !j.Deleted
where j.URLSlug.Equals(urlSlug)
select new KeyValuePair<string, int>(j.URLSlug, j.JourneyId)
);
次のエラーが発生します。
CS1662:ラムダ式をデリゲート型'System.Func <DataContext、string、System.Collections.Generic.KeyValuePair <string、int >>'に変換できません。これは、ブロック内の一部の戻り型がデリゲート型に暗黙的に変換できないためです。
KeyValuePair
コンパイルされたクエリからシングルを返すにはどうすればよいですか?それとも私はこれを完全に間違った方法で行っていますか?