locked
Select Distinct with AddRange in Entity Framework RRS feed

  • Question

  • User2012297617 posted

    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

    Thursday, August 15, 2019 5:45 PM

Answers

  • User-474980206 posted

    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()
    );

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 15, 2019 8:00 PM

All replies

  • User-2054057000 posted

    You need to apply the group by clause https://docs.microsoft.com/en-us/dotnet/csharp/linq/group-query-results

    Thursday, August 15, 2019 6:19 PM
  • User1120430333 posted
    IMO, the only thing you can do is run a Distinct on the collection of orders to make a new collection of orders.
    Thursday, August 15, 2019 6:21 PM
  • User2012297617 posted

    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?

    Thursday, August 15, 2019 6:32 PM
  • User475983607 posted

    One more column is required either an identity or sort.  Otherwise, 2222 can be VA or FL.

    Thursday, August 15, 2019 7:31 PM
  • User2012297617 posted

    right, which is good.   How do you recommend fixing this?

    viewModel.MyOrders.AddRange(responseModel.Data.Select(orders => orders).Distinct());

    Thursday, August 15, 2019 7:51 PM
  • User-474980206 posted

    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()
    );

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 15, 2019 8:00 PM
  • User2012297617 posted

    Thanks for your help!

    Thursday, August 15, 2019 8:06 PM
  • User1120430333 posted
    You would have to use a lambda expression on the Distinct.

    https://www.elevenwinds.com/blog/linq-distinctby-with-lambda-expression-parameter/
    Thursday, August 15, 2019 8:22 PM