我剛開始交手,在編譯的LINQ查詢,以及所遇到的一些奇怪的行爲返回KeyValuePair。從一個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: Cannot convert lambda expression to delegate type 'System.Func<DataContext,string,System.Collections.Generic.KeyValuePair<string,int>>' because some of the return types in the block are not implicitly convertible to the delegate return type
如何從編譯後的查詢中返回一個KeyValuePair
?或者我是以完全錯誤的方式進行討論的?
然後使用。首先()或。單(或類似的方法),以獲得您需要 – ry8806 2013-02-20 11:41:22
點值!而不是創建另一個包裝方法,我在()中包裝了我的初始查詢(在我的Func中),然後使用first或default。謝謝! – seanxe 2013-02-20 11:51:47