Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have class

C#
public class DB
{
    public string answer{ get; set; }
    public string question { get; set; }
    public string url { get; set; }
}



XML
List<DB> list1 = new List<DB>();
List<DB> list2 = new List<DB>();


How get all list elements, where DB.answer is not similar?
Posted

Write a simple Comparer. Then use Union and pass in the comparer


C#
public   class DBComparer : IEqualityComparer<DB>
    {

        public bool Equals(DB x, DB y)
        {
            return x.answer == y.answer;
        }

        public int GetHashCode(DB db)
        {
            return db.answer.GetHashCode();
        }
    }

   var filteredList = list1.Union(list2,new DBComparer()).ToList();
 
Share this answer
 
var result = list1.Where(a => list2.Any(b => a.answer != b.answer)).ToList();

-KR
 
Share this answer
 
Comments
George Swan 12-Nov-15 15:29pm    
Are you sure this is correct? I would have thought that it returns the whole of List1
BillWoodruff 12-Nov-15 15:37pm    
Any: "This method does not return any one element of a collection. Instead, it determines whether any elements of a collection satisfy a condition.

The enumeration of source is stopped as soon as the result can be determined."

Did you test this code ?
Try something like list1.Except(list2.select(x=>x.answer)).Union(list2.Except(list1.select(x=>x.answer)).

Basically this is based on the princple, a.Except(b).Union(b.Except(a) which suggests getting all items from lista not in listb and then all in listb not in lista and then putting a union on them.
 
Share this answer
 
Comments
CPallini 12-Nov-15 5:34am    
5.
Abhinav S 12-Nov-15 5:44am    
Thank you
Member 11266633 12-Nov-15 5:43am    
this code not work
Abhinav S 12-Nov-15 5:47am    
What did you try?
phil.o 12-Nov-15 7:01am    
'not working' is not a valid issue description

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900