我目前正在試圖從三個不同的表牽引結果,並將它們合併成一個列表,像這樣...LINQ的Concat的優化
var accountLogs = Context.AccountLogs.Where(s => s.Timestamp > fromDate).Select(s => new LogSummaryDto()
{
UserId = s.User.Id,
DisplayName = s.User.DisplayName
});
var spellLogs = Context.SpellLog.Where(s => s.Timestamp > fromDate).Select(s => new LogSummaryDto()
{
UserId = s.User.Id,
DisplayName = s.User.DisplayName
});
var generalLogs = Context.Log.Where(s => s.Timestamp > fromDate).Select(s => new LogSummaryDto()
{
UserId = s.User.Id,
DisplayName = s.User.DisplayName
});
var m1 = accountLog.Concat(spellLogs);
var m2 = m1.Concat(generalLogs).toList();
不過,我敢肯定,必須有使用LINQ to某種方式這是一個查詢,任何想法/指針?
如果該查詢返回正確的結果,那很好,由於LINQ的延遲執行,您只執行一個查詢在這裏。其實你甚至沒有在你的代碼中執行它。使用'foreach'或用'm1.Concat(generalLogs).ToList();'創建一個集合。然後查詢將被執行。 –
@ChrisF你可以做得更好:'m1 = accountLog.Concat(spellLog).Concat(generalLogs)',不使用'm2'! :)只是在開玩笑 –
@TimSchmelter已經回答了這個問題,迄今爲止最快的解決方案就是它的原型。 –