当集合不是空/空时,如何获取集合的CASE计数。 例如我要分析数据,如果用户在Payment IList / ICollection中至少有一项,则获取用户总数。
我的用户类别
public class User
{
public User()
{
Payments = new HashSet<Payment>();
Messages = new HashSet<Message>();
FileLockResults = new HashSet<FileLockResult>();
}
[Key]
public int ID { get; set; }
[Required]
public DateTime RegisteredDate { get; set; }
public virtual ICollection<Payment> Payments { set; get; }
}
JSON数据库临时文件:
[
{
"ID": "1",
"RegisteredDate": "1-JAN-2020",
"Payments": "null"
},
{
"ID": "2",
"RegisteredDate": "1-JAN-2020",
"Payments": [
{
"Amount" : "150"
}
]
},
{
"ID": "3",
"RegisteredDate": "1-JAN-2020",
"Payments": "null"
},
{
"ID": "4",
"RegisteredDate": "2-JAN-2020",
"Payments": "null"
}
]
我需要的结果是:
+------------+-----------------------+------------------------+
| Date | Total User Registered | User with Payments > 0 |
+------------+-----------------------+------------------------+
| 1-JAN-2020 | 3 | 1 |
+------------+-----------------------+------------------------+
| 2-JAN-2020 | 1 | 0 |
+------------+-----------------------+------------------------+
我暂时尝试过此操作:
var dataset = dbContext.Users.Include(usrr => usrr.Payments)
.GroupBy(x => x.RegisteredDate.Date)
.Select(x => new
{
Total_User_Registered = x.Count(),
Date = (DateTime)x.Key,
Premium_Users = x.Count(u=>u.Payments.Count()>0)
}).ToList();
问题: 无需付款ICollection / IList计数即可正常工作。但是,当控制台在带有Payments Icollection的ASP NET CORE API中进行呈现时,控制台将显示“ Payments Count”,只是继续滚动并显示以下相同错误:
选择[p0]。[UserID] 来自[付款] AS [p0] Microsoft.EntityFrameworkCore.Database.Command:信息:已执行DbCommand(640毫秒)[Parameters = [],CommandType ='Text',CommandTimeout = '30']