Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 443 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Difference between two lists

#1
I Have two generic list filled with CustomsObjects.

I need to retrieve the difference between those two lists(Items who are in the first without the items in the second one) in a third one.

I was thinking using `.Except()` was a good idea but I don't see how to use this..
Help!

Reply

#2
If both your lists implement IEnumerable interface you can achieve this using LINQ.

list3 = list1.where(i => !list2.contains(i));
Reply

#3
var third = first.Except(second);

(you can also call `ToList()` after `Except()`, if you don't like referencing lazy collections.)


The `Except()` method compares the values using the default comparer, if the values being compared are of base data types, such as `int`, `string`, `decimal` etc.

Otherwise the comparison will be made by object address, which is probably not what you want... In that case, make your custom objects implement `IComparable` (or implement a custom `IEqualityComparer` and pass it to the `Except()` method).
Reply

#4
Since the Except extension method operates on two IEumerables, it seems to me that it will be a O(n^2) operation. If performance is an issue (if say your lists are large), I'd suggest creating a HashSet<CustomsObject> from list1 and use HashSet's ExceptWith method.
Reply

#5
bit late but here is working solution for me

var myBaseProperty = (typeof(BaseClass)).GetProperties();//get base code properties
var allProperty = entity.GetProperties()[0].DeclaringType.GetProperties();//get derived class property plus base code as it is derived from it
var declaredClassProperties = allProperty.Where(x => !myBaseProperty.Any(l => l.Name == x.Name)).ToList();//get the difference

In above mention code I am getting the properties difference between my base class and derived class list
Reply

#6
You could do something like this:

var result = customlist.Where(p => !otherlist.Any(l => p.someproperty == l.someproperty));
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through