我已經完成了以下EF.core(3.1/3.2)查詢
var monthlySalesList = context.Bids.Include(r => r.AllRequest).ThenInclude(r => r.Category).Where(b => b.UID== UID && (b.Status == MyStatus.Awarded || b.Status == MyStatus.Completed))
.GroupBy(a => new { Month =a.InsertedDate.Month })
.Select(g => new MyServiceList()
{
Key = g.Key.ToString(),
Month = g.Key.Month.ToString(),
Total= g.Sum(s => s.totalBudget)
}).ToList();
我沒有得到所有月份在今年相反,它顯示只有2個月說(10,11)與total.In查詢Mystatus上面是一個ENUM階級和MyserviceList Model階級包含獲取和設定等key,month,sum and total。
我只得到
-----------------
Months total
------------------
10 1234
11 1212
我怎樣才能獲得零值的剩余月份。
-----------------
Months total
------------------
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 1234
11 1212
12 0
uj5u.com熱心網友回復:
- 定義另一個串列
- 在“monthlySalesList”之后創建一個 for 回圈并從串列中進行查詢。
- 如果你沒有得到資料,在新串列上設定 0 ,否則設定 value 。
例如 :
類:公共類 ReportData { 公共 int 月 { 獲取;放; } 公共整數總{ 得到;放; } }
代碼 :
List reportData = new List();
for (int i = 1; i <= 12; i )
{
try
{
Bids bids = new Bids();
bids = monthlySalesList.Where(t => t.month == i).FirstOrDefault();
if(bids == null)
{
reportData.Add(new ReportData() {
month = i,
total = 0
});
}
else
{
reportData.Add(new ReportData()
{
month = i,
total = bids.Month
});
}
}
catch(Exception ex)
{
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/359174.html
上一篇:如何使用EF5存盤列舉串列
