Remove pair in a list RRS feed

  • Question

  • Hello, 

    Im struggling to make a query that retrieve one once the items that have the same pair of value (code1 and code2 and reverse) from a table with 

    Id, Code1, Code2, InsertDate,

    i have to retrieve only first pair of values code1 and code2 , this also should be work for reverse pair (code2 and code1 is same of code1 and code 2)

    example if i have :

    Id , Code1, Code2, 

    1      AAA,    AAB,  

    2      AAA,    AAB,  

    3      AAA,    AAB, 

    4      AAB,    AAA,  

    5      AAB,    AAA

    i would have in my outcome only distinct pair also considering reverse pair, for example the item with id 1 its same of item with id 2 and 3 but also with 4 and 5 , because the pair its the same .

    i tried:

    var dest = source.Select(e1 => source.All(e2 => e1.Code1 == e2.Code2 && e1.Code2 == e2.Code1));

    but doesnt work, i dont have any item in dest.

    How i can do this? 


    Wednesday, August 12, 2020 11:00 AM

All replies

  • Hello,

    You could use a custom comparer which implements IEqualityComparer<T>.

    Using this class I want distinct customers by CompanyName, CountryIdentifier and City.

    public class Customer
        public int Id { get; }
        public int CustomerIdentifier { get; set; }
        public string CompanyName { get; set; }
        public string Street { get; set; }
        public string City { get; set; }
        public string PostalCode { get; set; }
        public int? CountryIdentfier { get; set; }

    The following is used to get distinct as per above. This is the key to everything working

    public class CustomerComparer : IEqualityComparer<Customer>
        public bool Equals(Customer customer1, Customer customer2)
            return customer1.CompanyName == customer2.CompanyName &&
                   customer1.CountryIdentfier == customer2.CountryIdentfier &&
                customer1.City == customer2.City;
        public int GetHashCode(Customer obj)
            return new { obj.CompanyName, obj.CountryIdentfier, obj.City }.GetHashCode();

    Test data

    private IEnumerable<Customer> CustomerList()
        return new List<Customer>()
            new Customer() {CompanyName = "Around the Horn", Street = "120 Hanover Sq.", City = "London",
                PostalCode = "WA1 1DP", CountryIdentfier = 19},
            new Customer() {CompanyName = "La maison d'Asie", Street = "1 rue Alsace-Lorraine", City = "Toulouse",
                PostalCode = "31000", CountryIdentfier = 8},
            new Customer() {CompanyName = "Around the Horn", Street = "120 Hanover Sq.", City = "London",
                PostalCode = "WA1 1DP", CountryIdentfier = 19}

    use it

    var customers = CustomerList()
        .Distinct(new CustomerComparer()).ToList();

    We are expecting two customers and that is what we get.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    • Proposed as answer by Cherkaoui.Mouad Wednesday, August 12, 2020 11:52 AM
    Wednesday, August 12, 2020 11:29 AM
  • Hi first100,
    I viewed that Kareninstructor has provide a good solution.
    So has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly. 
    Best Regards,
    Daniel Zhang

    "Windows Forms General" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Windows Forms General" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    Friday, August 14, 2020 9:36 AM
  • hi

     var test = (from i in destins
                           from j in destins.SkipWhile(j => j.code1 == i.code1 && j.code2 == i.code2)
                           where i.code1 != j.code1 && j.code1 != null
                           select j).ToList();

    Thanks and regards

    Friday, August 14, 2020 4:56 PM