Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have TableDE class that IdFirst, IdSecond and Name

What I have tried:

C#
<pre>                
var userEntityGroups = TableDE.GroupBy(x => new { x.IdFirst, x.IdSecond})
.OrderBy(g => g.Key.IdFirst).ThenBy(g => g.Key.IdSecond)
.Select(g => new
{
     IdFirst = g.Key.IdFirst,
     IdSecond= g.Key.IdSecond,
     TableDE= g.OrderBy(x => x.Name)
});

then i return Ok(userEntityGroups) //webApi response


Now when i debug user entity groups has 2 records
[0] where this has IdFirst, IdSecond and TableDE
[1] Also where this has IdFirst, IdSecond and TableDE

now in TableDE of [0] it has 2 records [0] and [1] where IdFirst and IdSecond are similar but name field for each one is different

Also in TableDE of [1] it has 2 records [0] and [1] where IdFirst and IdSecond are similar but name field for each one is different

I don't want return userEntityGroups to return 4 records i want it to only return 2 records

right now this is what i'm getting
IdFirst=1, IdSecond=1, Name=name11
IdFirst=1, IdSecond=1, Name=name12
IdFirst=2, IdSecond=2, Name=name21
IdFirst=2, IdSecond=2, Name=name22


This is what i want
IdFirst=1, IdSecond=1, Name=name11,name12
IdFirst=2, IdSecond=2, Name=name21,name22
Posted
Updated 15-Apr-20 23:14pm
v5
Comments
Maciej Los 16-Apr-20 4:09am    
What's your input data?

1 solution

Take a look at below example, which should help in resolving an issue.

C#
//LinqPad rules!
void Main()
{
	List<TableDE> data = new List<TableDE>()
	{
		new TableDE(){IdFirst=1, IdSecond=1, Name="name11"},
		new TableDE(){IdFirst=1, IdSecond=1, Name="name12"},
		new TableDE(){IdFirst=2, IdSecond=2, Name="name21"},
		new TableDE(){IdFirst=2, IdSecond=2, Name="name22"},
		new TableDE(){IdFirst=3, IdSecond=3, Name="name31"},
		new TableDE(){IdFirst=1, IdSecond=2, Name="name1221"}
	};

	var result = data
		.GroupBy(x=> new{ Id1 =  x.IdFirst,  Id2 = x.IdSecond})
		.Select(grp=> new
		{
			IdFirst = grp.Key.Id1,
			IdSecond = grp.Key.Id2,
			Names = string.Join(",", grp.OrderBy(y=>y.Name).Select(y=>y.Name)),
		})
		.OrderBy(x=>x.IdFirst)
		.ThenBy(x=>x.IdSecond)
		.ToList();
	result.Dump();

}

// Define other methods and classes here
public class TableDE
{
	public int IdFirst { get; set; }
	public int IdSecond { get; set; }
	public string Name { get; set; }
}


Result:
IdFirst IdSecond Names
1       1        name11,name12 
1       2        name1221 
2       2        name21,name22 
3       3        name31
 
Share this answer
 
v2
Comments
Member 14800672 16-Apr-20 5:11am    
what if there was two other records
IdFirst=3, IdSecond=3, Name=name31
IdFirst=1, IdSecond=2, Name=name1221

this should be there result:
IdFirst=1, IdSecond=1, Name=name11,name12
IdFirst=1, IdSecond=2, Name=name1221
IdFirst=2, IdSecond=2, Name=name21,name22
IdFirst=3, IdSecond=3, Name= name31

how i'm not going to string join these two? i just want to join related ones
Maciej Los 16-Apr-20 5:20am    
Just add these to my example and find out what happen...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900