3

I have this query:

    var rowsPerProvider = (from row in dt.Select()
        let emp = row["f_name"].ToString().Trim()
        group row by emp
        into g
        select g).ToDictionary(
            g => g.Key,
            g => g.ToArray());

How can I update it to also filter on some more columns? for example currently it is on f_name. How can I update it to group on f_name and m_name and l_name?

2 Answers2

3

Use anonymous class:

// (...)
    group row by new { emp, something }
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
3

Make an anonymous object containing the fields you want to group by:

var rowsPerProvider = (from row in dt.Select()
    group row by new
    {
        emp1 = row["f_name"].ToString().Trim(),
        emp2 = row["m_name"].ToString().Trim(),
        emp3 = row["l_name"].ToString().Trim(),
    }
    into g
    select g).ToDictionary(
        g => g.Key,
        g => g.ToArray());
demoncodemonkey
  • 11,730
  • 10
  • 61
  • 103
  • 1
    thanks, so when we do grouping this way, it ensures that all of them for each key of this dictionary have the same first name, middle name and last name. right? – CutHimSomeSlack Jul 22 '14 at 21:53
  • 2
    @CutHimSomeSlack - yes, the anonymous type has an Equals and GetHashCode that check all the properties are the same - see this answer: http://stackoverflow.com/a/12123542/383710 – DaveShaw Jul 22 '14 at 21:59