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 Answers 2

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()); 
Sign up to request clarification or add additional context in comments.

2 Comments

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 - yes, the anonymous type has an Equals and GetHashCode that check all the properties are the same - see this answer: stackoverflow.com/a/12123542/383710
3

Use anonymous class:

// (...) group row by new { emp, something } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.