I have a list and i'm trying to remove all the duplicates from it using .Distinct() but i can't get it to work.
A simple class:
public class Street
{
public string Name;
public string Location;
}
Here i create a new instance and add to it:
List<Street> Streets = new List<Street>();
Streets.Add(new Street { Name = "Street1", Location = "District1" });
Streets.Add(new Street { Name = "Street1", Location = "District1" });
Streets.Add(new Street { Name = "Street2", Location = "District2" });
Create a distinct instance:
List<Street> DistinctStreets = Streets.Distinct().ToList();
Loop through the list:
foreach (Street street in DistinctStreets)
{
HttpContext.Current.Response.Write("<p>" + street.Name + "</p>");
}
However, this returns
Street1
Street1
Street2
But i was expecting
Street1
Street2
Can anyone explain what's going wrong and why my distinct instance isn't distinct?
thanks