0

Say I have an entity with following properties [Id, UserName, ProductName], where Id is the PK, and other fields are not unique, so the rows with same UserName repeat multiple times.

For one of the views I need to get a collection that would have unique UserName, and other fields would be combined together using string concatenation or something similar.

If I have

[0, John, Alpha]
[1, Mary, Beta]
[2, John, Gamma]

I need a query that would get me a collection like

[John, Alpha Gamma]
[Mary, Beta]

And it would be awesome if all that could be accomplished on the database side without loading the entities.

1 Answer 1

2

You are looking for GroupBy():

var results = context.MyEntities.GroupBy( x => x.UserName);

foreach (var item in results)
{
    Console.WriteLine("{0} : {1}", item.Key, string.Join(",", item.Select( x=> x.ProductName));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.