none
IEnumerable<IGrouping> and Where clause RRS feed

  • Question

  • I have a result of

    IEnumerable<IGrouping<Person, Call>>

    for each person all its calls.

    I want tp continue filtering the result. for example return a group of persons and their open calls only so if the current results are

    person call.count()

    "test"   10

    "test2"  5

     

    I would like to get only the open calls for each person as

    IEnumerable<IGrouping<Person, Call>>

    and the results are something like

    person call.count()

    "test"   4

    "test2"  0

     

    how can I do this?



    Saturday, July 30, 2011 12:24 PM

Answers

  • Hi liorba

    The following code snippet should give you what you want.

    var areOpen = from r in result
           from o in r
           where o.isOpen
           group o by o.ownwer into oGrp
           orderby oGrp.Count() descending
           select oGrp;
    

    The above query is assuming that o.isOpen is of type bool, if it is NOT of type bool then you will need to change that line so that the expression is evaluated to a true / false, for example :

    where o.isOpen == "Open"

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by liorba Saturday, July 30, 2011 4:43 PM
    Saturday, July 30, 2011 2:57 PM
  • Hi liorba;

    If I understand you correctly this should work

    var areOpen = from r in result 
           from o in r 
           where o.isOpen 
           group o by o.ownwer into oGrp 
           orderby oGrp.Count() descending 
           select new
           {
             PeopleGroup = oGrp,
             PeopleCount = oGrp.Count()
           };
    
           
    PeopleGroup is IEnumerable<IGrouping<...>>
    PeopleCount is is an integer
    
    public class PeopleGroupAndCount
    {
      public IGrouping<XXX, Call> PeopleGroup { get; set; }
      public int PeopleCount {get; set; }
    }
    
    Where XXX in the above class is the data type of o.ownwer
    


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by liorba Sunday, July 31, 2011 4:56 PM
    Sunday, July 31, 2011 2:01 PM

All replies

  • Can you please re-state your question it is not clear at all to me what you are asking.

    Also please post the query your working.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, July 30, 2011 2:04 PM
  • I made a previous grouping:

            public IEnumerable<IGrouping<person, Call>> TechniciansCloseCalls()
            {
                var allCalls = icd.GetAll();
                var result = from p in allCalls
                              group p by p.ownwer
                                 into g
                                 orderby g.Count() descending
                                 select g;
                return result;
            }

    each call has a propertyis open

    let assume the result is

    person call.count()

    "test"   10

    "test2"  5

     

    now on the result I would like to filter and get the same group but only for open calls that is something like:

    result.where (c => c.isOpen)

    and get filter result but I fon't know the syntax for it.

     

     


    Saturday, July 30, 2011 2:11 PM
  • Hi liorba

    The following code snippet should give you what you want.

    var areOpen = from r in result
           from o in r
           where o.isOpen
           group o by o.ownwer into oGrp
           orderby oGrp.Count() descending
           select oGrp;
    

    The above query is assuming that o.isOpen is of type bool, if it is NOT of type bool then you will need to change that line so that the expression is evaluated to a true / false, for example :

    where o.isOpen == "Open"

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by liorba Saturday, July 30, 2011 4:43 PM
    Saturday, July 30, 2011 2:57 PM
  • Thanks! that worked indeed.
    Saturday, July 30, 2011 4:43 PM
  • Not a problem, glad I was able to help.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, July 30, 2011 5:11 PM
  • I have additional question:

    now I would like to get for each person the count it self the is:

    instead of

    IEnumerable<IGrouping<person, Call>>

    and then iterate over it and call count for each person I would like to get

    IEnumerable<IGrouping<person, int>>

    but if I change the query to

    var areOpen = from r in result
    from o in r
    where o.isOpen
    group o by o.ownwer into oGrp
    orderby oGrp.Count() descending
    select oGrp.Count()  ;

     

    I get

    IEnumerable<int>

    and not a grouping at all  for each person.

    it doesn't even need to be a qrouping but I need to return it via a method on diffrent layer so anynymos type is quit a problem if I'm not mistaken so a grouping of person,int will be preferable

     


    Sunday, July 31, 2011 10:21 AM
  • Hi liorba;

    Are you saying that you want a single integer for each group? or a single integer for all the group totals?

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Sunday, July 31, 2011 1:02 PM
  •  single integer for each group that just represent the total count of calls for each group. each person in the group has let say 5 calls. I would like to get the number itself.

    IEnumerable<IGrouping<person, int>>

    person    int

    "tst"          1

    "tst2"         0

     

    and so on.. 

    I could total each count to get the total for the entire groups

    Sunday, July 31, 2011 1:05 PM
  • Hi liorba;

    If I understand you correctly this should work

    var areOpen = from r in result 
           from o in r 
           where o.isOpen 
           group o by o.ownwer into oGrp 
           orderby oGrp.Count() descending 
           select new
           {
             PeopleGroup = oGrp,
             PeopleCount = oGrp.Count()
           };
    
           
    PeopleGroup is IEnumerable<IGrouping<...>>
    PeopleCount is is an integer
    
    public class PeopleGroupAndCount
    {
      public IGrouping<XXX, Call> PeopleGroup { get; set; }
      public int PeopleCount {get; set; }
    }
    
    Where XXX in the above class is the data type of o.ownwer
    


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by liorba Sunday, July 31, 2011 4:56 PM
    Sunday, July 31, 2011 2:01 PM
  • Thanks again! :)
    Sunday, July 31, 2011 4:56 PM
  • Not a problem, always glad to help.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, August 1, 2011 3:14 AM