I have the following model:
public class Result
{
public int Id { get; set; }
public string Company { get; set; }
}
And I have a List with data similar to the following:
Id Company ================= 21 Microsoft 22 Apple 22 IBM 23 Microsoft
How can I use Linq to give me the distinct ID's, concatenating the Company column with a delimiter?
My output should be:
Id Company ================= 21 Microsoft 22 Apple, IBM 23 Microsoft
You can use GroupBy
and String.Join
:
IEnumerable<Result> query = results.GroupBy(r => r.Id)
.Select(g => new Result
{
Id = g.Key,
Company = String.Join(", ", g.Select(r => r.Company))
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With