从 Entity Framework 核心查询中获取一年中的所有月份
Getting all months in an Year from Entity Framework core query
我已完成以下 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
class 和 MyserviceList Model
class 包含 get & set 例如 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
- 定义另一个列表
- 在“monthlySalesList”之后进行for循环并从列表中进行查询。
- 如果没有得到数据,在新列表上设置0,否则设置值。
例如:
Class :
public class 报告数据
{
public int month { 得到;放; }
public 总计 { 得到;放; }
}
代码:
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.total
});
}
}
catch(Exception ex)
{
}
}
我已完成以下 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
class 和 MyserviceList Model
class 包含 get & set 例如 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
- 定义另一个列表
- 在“monthlySalesList”之后进行for循环并从列表中进行查询。
- 如果没有得到数据,在新列表上设置0,否则设置值。
例如:
Class : public class 报告数据 { public int month { 得到;放; } public 总计 { 得到;放; } }
代码:
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.total
});
}
}
catch(Exception ex)
{
}
}