C# 获取特定标准和其他标准的计数和平均值

C# 获取特定标准和其他标准的计数和平均值,c#,linq,C#,Linq,我的数据格式如下 UserId Property1 Property2 Property3 Testval 1 1 1 10 35 2 1 2 3 45 3 2 5 6 55 and

我的数据格式如下

    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
我需要的是符合这些标准的用户数&testval平均数,以及所有其他不符合这些标准的用户数

因此,结果数据结构如下所示

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和avgval,更重要的是,对于其他用户来说也是如此

我们衷心感谢您的帮助


感谢

获取特定标准的计数/平均值,这很容易

Func<MyUser, boolean> criterion1 = user => user.Property1==1;
var avg = data.Where(criterion1).Average(user => user.Testval);
var count = data.Where(criterion1).Count();

要获得特定标准的计数/平均值,很容易

Func<MyUser, boolean> criterion1 = user => user.Property1==1;
var avg = data.Where(criterion1).Average(user => user.Testval);
var count = data.Where(criterion1).Count();

看起来您正在寻找按标准分组。大概是这样的:

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();

看起来您正在寻找按标准分组。大概是这样的:

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();

您可以使用select new返回包含您的条件的新匿名类型对象

public void Test()
    {
        var list = new List<User>();
        list.Add(new User {UserId = 1, Property1 = 1, Property2 = 1, Property3 = 10, Testval = 35});
        list.Add(new User {UserId = 1, Property1 = 2, Property2 = 2, Property3 = 3, Testval = 45});
        list.Add(new User {UserId = 1, Property1 = 5, Property2 = 5, Property3 = 6, Testval = 55});

        Func<User, bool> crit = u => u.Property1 == 1 & u.Property3==10;
        var zz = list.Where(crit)
            .GroupBy(t => new {ID = t.UserId})
            .Select(w => new
            {
                average = w.Average(a => a.Testval),
                count = w.Count(),
                rest = list.Except(list.Where(crit)).Average(a => a.Testval)
            }).Single();
    }
公共无效测试()
{
var list=新列表();
添加(新用户{UserId=1,Property1=1,Property2=1,Property3=10,Testval=35});
添加(新用户{UserId=1,Property1=2,Property2=2,Property3=3,Testval=45});
添加(新用户{UserId=1,Property1=5,Property2=5,Property3=6,Testval=55});
Func crit=u=>u.Property1==1&u.Property3==10;
var zz=列表,其中(临界值)
.GroupBy(t=>new{ID=t.UserId})
.选择(w=>new
{
平均值=w.average(a=>a.Testval),
count=w.count(),
rest=list.Except(list.Where(crit)).Average(a=>a.Testval)
}).Single();
}

您可以使用“选择新建”返回包含您的条件的新匿名类型对象

public void Test()
    {
        var list = new List<User>();
        list.Add(new User {UserId = 1, Property1 = 1, Property2 = 1, Property3 = 10, Testval = 35});
        list.Add(new User {UserId = 1, Property1 = 2, Property2 = 2, Property3 = 3, Testval = 45});
        list.Add(new User {UserId = 1, Property1 = 5, Property2 = 5, Property3 = 6, Testval = 55});

        Func<User, bool> crit = u => u.Property1 == 1 & u.Property3==10;
        var zz = list.Where(crit)
            .GroupBy(t => new {ID = t.UserId})
            .Select(w => new
            {
                average = w.Average(a => a.Testval),
                count = w.Count(),
                rest = list.Except(list.Where(crit)).Average(a => a.Testval)
            }).Single();
    }
公共无效测试()
{
var list=新列表();
添加(新用户{UserId=1,Property1=1,Property2=1,Property3=10,Testval=35});
添加(新用户{UserId=1,Property1=2,Property2=2,Property3=3,Testval=45});
添加(新用户{UserId=1,Property1=5,Property2=5,Property3=6,Testval=55});
Func crit=u=>u.Property1==1&u.Property3==10;
var zz=列表,其中(临界值)
.GroupBy(t=>new{ID=t.UserId})
.选择(w=>new
{
平均值=w.average(a=>a.Testval),
count=w.count(),
rest=list.Except(list.Where(crit)).Average(a=>a.Testval)
}).Single();
}

Try
data.Where(w=>w.Property1==1&&w.Property3==10)。平均值(r=>r.Testval)如果userId是唯一的,那么您可以获得标准a和b的结果中不存在的rest。temp datatable听起来很有用。@M.Wiśnicki好的,但是我如何在结果数据中获得其余条件以及不属于任何条件的其他条件structure@Badiparmagi是的,用户ID是唯一的。这些标准是互斥的吗?可能是的,否则我看不出如何定义“其余部分”。尝试
data.Where(w=>w.Property1==1&&w.Property3==10)。Average(r=>r.Testval)如果userId是唯一的,那么您可以获得标准a和b的结果中不存在的rest。temp datatable听起来很有用。@M.Wiśnicki好的,但是我如何在结果数据中获得其余条件以及不属于任何条件的其他条件structure@Badiparmagi是的,用户ID是唯一的。这些标准是互斥的吗?可能是的,否则我看不出你如何定义“其余的”。哦。。ok,然后Criteria=-1为其余部分提供值。。是这样吗……是的。当然,你可以使用任意数字,甚至像“a”,“b”,“rest”这样的字符串来代替0,1,…,-1。。ok,然后Criteria=-1为其余部分提供值。。是这样吗……是的。当然,您可以使用任意数字,甚至像“a”、“b”、“rest”这样的字符串,而不是0、1、--1。通过计算对rest值进行编辑。通过计算对rest值进行编辑。