我的数据格式如下..
UserId Property1 Property2 Property3 Testval
1 1 1 10 35
2 1 2 3 45
3 2 5 6 55
and so on..
我有几个标准,下面是几个例子..
a) Where Property1=1 and Property3=10
b) Where Property1!=1 and Property2=5
我需要的是符合这些标准的用户数和测试平均值,以及所有其他不符合这些标准的用户数。
因此,结果数据结构如下..
User Count
Criteria Users
a 100
b 200
rest 1000
TestVal Average
Criteria avg
a 25
b 45
rest 15
我知道如何分别获取特定条件的用户列表。
data.Where(w=>w.Property1==1).Select(s=>s.UserId).ToList()
但是我如何获得 usercount 和 avg val 以及更重要的是对于其他用户来说相同。
真诚感谢任何帮助
谢谢
最佳答案
看起来您正在寻找按条件分组。像这样:
var result = data.GroupBy(x =>
x.Property1 == 1 && x.Property3 == 10 ? 0 :
x.Property1 != 1 && x.Property2 == 5 ? 1 :
// ...
-1)
.Select(g => new
{
Criteria = g.Key,
Users = g.Count(),
Avg = g.Average(x => x.Testval),
})
.ToList();
关于c# - 获取特定标准和其他标准的计数和平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41344477/