c# - How to remove items from a List of Structs that exist in another List -
public struct registryapp { public string vendorname; public string name; public string version; }
i have 2 list<registryapp>
hold applications installed on windows box. why two? have 1 list hold x86
applications , 1 hold x64
applications.
list<registryapp> x64apps64list = new list<registryapp>(); list<registryapp> x64apps32list = new list<registryapp>();
once 2 populated appropriate data retrieved registry, try following make sure there no duplicates. worked decently on list<string>
not working list<registryapp>
.
list<registryapp> listofallappsinstalled = new list<registryapp>(); ienumerable<registryapp> x86apps = x64apps32list.except(x64apps64list); ienumerable<registryapp> x64apps = x64apps64list.except(x64apps32list); foreach (registryapp regitem in x86apps) { if ((regitem.name != null) && (regitem.name.length > 2) && (regitem.name != "")) { listofallappsinstalled.add(regitem); } } foreach (registryapp regitem in x64apps) { if ((regitem.name != null) && (regitem.name.length > 2) && (regitem.name != "")) { listofallappsinstalled.add(regitem); } }
any way pull off?
edited
to remove items list of structs exist in list can see solution provided cuong le
here :
by using distinct parameterless extension method on list type, can remove duplicate elements.
then, can optionally invoke tolist extension actual list duplicates removed.
static void main() { // list duplicate elements. list<int> mylist = new list<int>(); mylist.add(1); mylist.add(2); mylist.add(3); mylist.add(3); mylist.add(4); mylist.add(4); mylist.add(4); foreach (int value in mylist) { console.writeline("before: {0}", value); } // distinct elements , convert list again. list<int> distinct = mylist.distinct().tolist(); foreach (int value in distinct) { console.writeline("after: {0}", value); } }
if answer has solved problem click accept solution button, doing others know solution.
Comments
Post a Comment