我正在使用 .NET 6,我有一個類“projectStatistics”,其中包含給定日期的專案統計資訊。
public class ProjectStatistics {
public int ProjectStatisticsId {get; set;}
public Project Project {get; set}
public DateTime Date {get; set}
public int statistic1 {get; set}
public int statistic3 {get; set}
...
...
}
由于查詢此結果是資源密集型的,因此每 24 小時在后臺執行一次。然后將結果存盤在資料庫中,然后可以使用該資料庫創建圖表。
現在我正在努力解決的問題是,例如,當資料庫中有 1000 行時,我不想用所有這些記錄制作圖表,假設我想制作包含 50 個日期的圖表(資料庫中的記錄)
然后我想回傳表中行 % 20 == 0 的所有行,問題是我不能使用 rowId 檢查行 % x == 0。因為表包含多個專案的統計資訊。
我在堆疊溢位中發現了這個問題:Split one list into multiple lists by divisible number
我有這個 LINQ 陳述句:
var result = projectStatistics
.Where(s => s.Project == project) // Note: project is a repo method parameter
.Select( (value, index) => new
{
value,
index
})
.GroupBy(item => item.index % 20, item => item.value) // Note: 20 is a repo method parameter.
.Select(chunk => chunk.FirstOrDefault()).AsEnumerable();
只有這個查詢給了我以下錯誤:
The LINQ expression 'DbSet<ProjectStatistics>()
.Where(s => s.Project == __project_0)
.Select((value, index) => new {
value = value,
index = index
})' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
uj5u.com熱心網友回復:
我認為問題LINQ可能Entities不明白如何轉換該Select多載(您正在使用)
我們可以從 MSDN Supported and Unsupported LINQ Methods (LINQ to Entities)中看到
Select不支持:IQueryable Select<TSource, TResult>( this IQueryable source, Expression<Func<TSource, int, TResult>> 選擇器)
我們可以AsEnumerable()在你使用Select多載之前使用
根據您的邏輯,我做了一些修改以嘗試讓代碼簡單
var result = projectStatistics
.Where(s => s.Project == project)
.AsEnumerable()
.Select( (value, index) => new
{
value,
grp = index % 20
})
.Where(s => s.grp == 0);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/462710.html
