Home > DeveloperSection > Forums > How to remove from one list all the items in another?
Goti Bandu

Total Post:119

Points:835
Posted on    June-13-2013 8:05 AM

 VB.Net VB.Net 
Ratings:


 1 Reply(s)
 1860  View(s)
Rate this:
Hi,

I'm reading a set of rows from a remote database, and a similar set from a local database, and then using RemoveAll to get rid of remote rows that are already present locally...

remote_events = (From a In remote.Events
                          Where ...etc...).ToList
local_events = (From a In local.Events
                            Where ...etc...).ToList
remote_events.RemoveAll(Function (ByVal event_row As Remote_Event)
    (From a In local_events Where a.Identifier = event_row.Identifier).Count > 0)

but this doesn't seem right to me. I don't think I should be counting things when all I really need to do is check whether a match exists. I've tried various usages of IsDBNull and IsNothing but get nonsense. I can't use .Except (as suggested here) because the list elements are not the same type.

Is there a better way of doing this?

Thanks in advance for any recommendations or solutions.


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    June-14-2013 1:40 AM

Hi Goti,

You can compare the Identifier as following line of code

    Remote_Events = (From r_evt In Remote_Events
                    Where Not ((From l_evt In Local_Events Select l_evt.Identifier).Contains(r_evt.Identifier))
                    Select r_evt).ToList

I hope this helps and at least moves you in the right direction.


Don't want to miss updates? Please click the below button!

Follow MindStick