[英]How to simplify LINQ query just to filter out some special rows
我對Entity Framework和LINQ非常不熟悉。 我有一個單獨的實體設置有一些列,我想過濾我們的一些特殊行。 其中4行命名為Guid(字符串),Year(短),Month(短)和FileIndex(短)。 我想獲得Guid-Year-Month的每個現有組合具有最大FileIndex的所有行。
我目前的解決方案如下:
var maxFileIndexRecords = from item in context.Udps
group item by new { item.Guid, item.Year, item.Month }
into gcs
select new { gcs.Key.Guid, gcs.Key.Year, gcs.Key.Month,
gcs.OrderByDescending(x => x.FileIndex).FirstOrDefault().FileIndex };
var result = from item in context.Udps
join j in maxFileIndexRecords on
new
{
item.Guid,
item.Year,
item.Month,
item.FileIndex
}
equals
new
{
j.Guid,
j.Year,
j.Month,
j.FileIndex
}
select item;
我認為應該有一個更短的解決方案,更多的性能。 有人對我有暗示嗎? 謝謝
你很親密 沒有必要實際選擇分組鍵。 您只需選擇每個組的第一項:
var maxFileIndexRecords =
from item in context.Udps
group item by new { item.Guid, item.Year, item.Month }
into gcs
select gcs.OrderByDescending(x => x.FileIndex).FirstOrDefault();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.