Blazor & EF Core:SumAsync 不会每次都计算吗?



我的存储库中有以下代码:

for (int i = startYear; i <= (endYear); i++)
{
try
{
int varde = await _db.TomtbehovYear
.Where(m => m.Tomtbehov.Kommun.Namn == namn && m.Year == i)
.Select(m => m.Antal)
.SumAsync();
listan.Add(varde);
}
catch
{
listan.Add(0);
}
}

还有我的子组件:

<td>@Area</td>
@foreach (var ant in Antal)
{
<td>@ant</td>
}
@code {
[Parameter]
public string Area { get; set; } = null;
[Parameter]
public string AreaTyp { get; set; } = null;
[Parameter]
public int StartYear { get; set; } 
[Parameter]
public int EndYear { get; set; }
private List<int> Antal = new List<int>();
protected override async Task OnInitializedAsync()
{
Antal = await TomtbehovYearRepository.GetYearSumAsync(StartYear, EndYear, AreaTyp, Area);
}
protected override async Task OnParametersSetAsync()
{
Antal = await TomtbehovYearRepository.GetYearSumAsync(StartYear, EndYear, AreaTyp, Area);
}
}

父级:

@if (raden.Kommun != Kommun)
{
Kommun = raden.Kommun;
<tr class="table-info"><TomtBehovSumma Area="@Kommun" AreaTyp="Kommun" StartYear="@StartYear" EndYear="@EndYear" /></tr>
}

但并不是每一个‘年’都是每一次都计算出来的,有些得到了价值;0";,如果我刷新我的页面;0";上一次现在是正确的,而其他时间是"正确的";0〃;。

我搞不明白——怎么了?

使用以下代码解决问题:

sumPerYear = await _db.TomtbehovYear
.Include(t => t.Tomtbehov)
.Where(x => x.Tomtbehov.Kommun.Namn == namn)
.GroupBy(x => x.Year)
.Select(yearGroup => new
{
Year = yearGroup.Key,
sumPerYear = yearGroup.Sum(t => t.Antal)
})
.ToListAsync();

相关内容

  • 没有找到相关文章

最新更新