简体   繁体   English

分组项目和计数

[英]Group items and count

My table is like below 我的桌子如下

Main_ID | Child_ID | Activity |
1       |1         | Start    
1       |2         | Stop    
1       |3         | Stop    
2       |1         | Start
2       |3         | Stop

What I'm looking for is like 我正在寻找的是

Main_ID | Start | Stop
1       |1      |2
2       |1      |1 

So, in first column I want to list all the Main_Ids and distinct Activities horizontally. 因此,在第一列中,我想要水平列出所有Main_Ids和不同的活动。 After that I want to display the count of distinct Child_Ids under each activity for the Main_ID. 之后,我想在Main_ID的每个活动下显示不同的Child_Ids的计数。

What I have tried is 我试过的是

var result = from q in db.Table
    where q.Child_ID != null
    group 1 by new { q.Main_ID,q.Child_ID, q.Activity } into g
    select new MyList
    {
        Main_ID = g.Key.Main_ID,
        Child_ID = g.Key.Child_ID,
        Activity = g.Key.Activity,
        Count = g.Count()
    }
    into p orderby p.Main_ID,p.Child_ID select p;

But with this I'm not getting what I have stated. 但有了这个,我没有得到我所说的。

I have achieved for the Child_ID using following 我使用以下内容实现了Child_ID

var result2 = result.GroupBy(x => x.Child_ID)
    .Select(grp => new MyList2
    {
        Child_ID = grp.Key,
        StartCount = grp.Where(x => x.Activity == "Start")
            .Select(x => x.Count).Cast<Int32>().FirstOrDefault(),
        StopCount = grp.Where(x => x.Activity == "Stop")
            .Select(x => x.Count).Cast<Int32>().FirstOrDefault()
    });

Not sure how to display it against Main_ID 不知道如何针对Main_ID显示它

Considering this class: 考虑这个课程:

public class Foo
{
    public int Main_ID {get;set;}
    public int Child_ID {get;set;}
    public string Activity {get;set;}
}

With this data: 有了这些数据:

var ls=new List<Foo>
    {
        new Foo{Main_ID=1,Child_ID=1,Activity="Start"},
        new Foo{Main_ID=1,Child_ID=2,Activity="Stop"},
        new Foo{Main_ID=1,Child_ID=3,Activity="Stop"},
        new Foo{Main_ID=2,Child_ID=1,Activity="Start"},
        new Foo{Main_ID=2,Child_ID=3,Activity="Stop"},
    };

You could do this: 你可以这样做:

var result = ls
    .GroupBy (g =>g.Main_ID)
    .Select (g =>new 
    {
       Main_ID = g.Key,
       Start = g.Where(s=>s.Actvity == "Start").Select(x => x.Child_ID).Distinct().Count(),
       Stop = g.Where(s=>s.Actvity == "Stop").Select(x => x.Child_ID).Distinct().Count(),
    } 
    ).ToList();

Result: 结果:

Main_ID Start Stop
1       1     2 
2       1     1 

All, 所有,

Thanks for your help. 谢谢你的帮助。

I could do that using following 我可以使用以下内容

 var v = from q in myTable
                where q.Child_ID != null
                orderby q.ModifiedDate descending
                select q;

        var result2 = v
            .GroupBy(g => new
        {
            g.Main_ID
        })
         .Select(grp => new MyClass
         {
             Main_ID = grp.Key.Main_ID,
             StartCount = grp.Where(x=> x.Activity == "Start").Select(x => x.Child_ID).Distinct().Count(),
             StopCount = grp.Where(x => x.Activity == "Stop").Select(x => x.Child_ID).Distinct().Count()
         });

I'm getting expected resuts using this. 我正在使用这个获得预期的resuts。

Now; 现在; I would like to know if this is correct approach and if the two queries can be merged. 我想知道这是否是正确的方法,如果两个查询可以合并。

As it had code, did not post it using comment; 由于它有代码,没有使用评论发布; sorry for that :-) 对不起:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM