I got this single line of code: viewModel.MyOrders.AddRange(responseModel.Data.Select(orders => orders));
I receive all the orders. If there are more than one row for the same order, I'd like to select the first one. That way I don't get something like this:
Orders:
1111 UT
2222 VA
2222 FL
3333 CA
Instead, I need to get something like this:
1111 UT
2222 VA
3333 CA
Helping you always. Don't forget to click "Mark as Answer" on the post that helped you.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
if you group by orderid (or whatever the key is), then you will get a grip for each order and in this group you will get a collection of the dups. you then need to summarize this collection to one row, say using first(), or summations.
var orders = response.Model.Data.GroupBy
(
r => r.OrderIdId,
r => r,
(key, g) => g.First()
);
Member
97 Points
418 Posts
Select Distinct with AddRange in Entity Framework
Aug 15, 2019 05:45 PM|lesponce|LINK
I got this single line of code: viewModel.MyOrders.AddRange(responseModel.Data.Select(orders => orders));
I receive all the orders. If there are more than one row for the same order, I'd like to select the first one. That way I don't get something like this:
Orders:
1111 UT
2222 VA
2222 FL
3333 CA
Instead, I need to get something like this:
1111 UT
2222 VA
3333 CA
Participant
1253 Points
935 Posts
Re: Select Distinct with AddRange in Entity Framework
Aug 15, 2019 06:19 PM|yogyogi|LINK
You need to apply the group by clause https://docs.microsoft.com/en-us/dotnet/csharp/linq/group-query-results
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Contributor
4923 Points
4198 Posts
Re: Select Distinct with AddRange in Entity Framework
Aug 15, 2019 06:21 PM|DA924|LINK
Member
97 Points
418 Posts
Re: Select Distinct with AddRange in Entity Framework
Aug 15, 2019 06:32 PM|lesponce|LINK
I tried this, but do I need to add the name of the order field?
viewModel.MyOrders.AddRange(responseModel.Data.Select(orders => orders).Distinct());
How the query will identify that I need distinct order number, even if I have different States within the data?
All-Star
52971 Points
23571 Posts
Re: Select Distinct with AddRange in Entity Framework
Aug 15, 2019 07:31 PM|mgebhard|LINK
One more column is required either an identity or sort. Otherwise, 2222 can be VA or FL.
Member
97 Points
418 Posts
Re: Select Distinct with AddRange in Entity Framework
Aug 15, 2019 07:51 PM|lesponce|LINK
right, which is good. How do you recommend fixing this?
viewModel.MyOrders.AddRange(responseModel.Data.Select(orders => orders).Distinct());
All-Star
58124 Points
15640 Posts
Re: Select Distinct with AddRange in Entity Framework
Aug 15, 2019 08:00 PM|bruce (sqlwork.com)|LINK
if you group by orderid (or whatever the key is), then you will get a grip for each order and in this group you will get a collection of the dups. you then need to summarize this collection to one row, say using first(), or summations.
Member
97 Points
418 Posts
Re: Select Distinct with AddRange in Entity Framework
Aug 15, 2019 08:06 PM|lesponce|LINK
Thanks for your help!
Contributor
4923 Points
4198 Posts
Re: Select Distinct with AddRange in Entity Framework
Aug 15, 2019 08:22 PM|DA924|LINK
https://www.elevenwinds.com/blog/linq-distinctby-with-lambda-expression-parameter/