GroupBy and Count Extension Method RRS feed

  • Question

  • Hello,
    I have a collection that looks as following:

    SomeId SomeDescription Count
    1           Description1       null
    2           Description1       null
    1           Description1       null
    1           Description3       null
    10         Description2       null

    What I'm trying to do is create an extension method that will perform a distinct on the collection, then group by Description and finally set the Count property on the original collection (Count is in my BaseObject so I should be able to access it in order to set it just not sure how to continue with the Join).

    So my original collection becomes:
    SomeId SomeDescription Count
    1           Description1       2
    2           Description1       2
    1           Description1       2
    1           Description3       1
    10         Description2       1

    This is what I have so far.
           public static void ComputeCounts<T, TKey>(
    this List<T> listToUpdate,
    Func<T, object> distintKeySelector,
    Expression<Func<T, TKey>> groupByKeySelector
    where T : BankMove.Models.BaseObject
    listToUpdate =
    .Select(selector => selector)
    .Join(listToUpdate, groupByKeySelector, groupByKeySelector,
    And I'd like to call it as follows:

    sourceCollection.ToList<SomeType>().ComputeCounts(obj => obj.FromID, obj => obj.Description)
    Any help is appreciated.

    Saturday, October 17, 2009 2:31 PM