Home > Enterprise >  C# Create an object of related items inside object
C# Create an object of related items inside object

Time:04-26

I have many rows coming back from a database. Its currently bringing back something like this

User,       Name,      shorter name,   number
Person 1,   Alpha,     A,              1
Person 1,   Beta,      B,              2
Person 1,   Charlie,   C,              0
Person 2,   Alpha,     A,              1

How do I make it so that it returns a new variable, with just one list for each person, which includes the array for Name, shorter Name and number?

I tried a toDictionary, with the user as key, but I couldnt get the array to work with that

CodePudding user response:

With Linq, try use GroupBy and then ToDictionary

DataSet.GroupBy(allUser => allUser.User).ToDictionary(groupedUser => groupedUser.User)

Or a better approach, just GroupBy

DataSet.GroupBy(allUser => allUser.User)
  • Related