0

I need to get a list of unique shipping addresses.

var shippingAddresses = ShopApi.Checkout.GetShippingAddresses(Shop.CommerceContext.AccountId).ToList();

This will give me a list of shipping addresses objects, but two of them have the same value for the Id column. How do I filter that or gets only one value to the list?

My ShippingAddress object looks like this.

public string Id { get; set; }
public string CustomerId { get; set; }
public string Address { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string CountryId { get; set; }
samithagun
  • 664
  • 11
  • 25

2 Answers2

1

I have a solution for your situation. You can filter your list again to only get 1 row for each Id value

public class ShippingAddresses
{
    public string Id { get; set; }
    public string CustomerId { get; set; }
    public string Address { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string CountryId { get; set; }
}

List<ShippingAddresses> shippingAddresses = new List<ShippingAddresses>();

//This statement will help you only get 1 row for each ID value
shippingAddresses = shippingAddresses.GroupBy(p => p.Id).Select(p => p.First()).ToList();
Hieu Le
  • 1,042
  • 8
  • 18
0

Somewhere in that ShippingAddress object, there is an underlying database query. That query needs to be modified to only include the unique results ... like this:

(from dbo in database.ShippingAddress
 select dbo.Id).Distinct()
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Michael Sims
  • 2,360
  • 1
  • 16
  • 29