I am using silverlight 4. I have a scenario where i need to compare two lists based on value.
List<Option> lstoption1 = new List<Option>();
Option opt = new Option();
opt.OptionText = "Text1";
lstoption1.add(opt)
Option opt2 = new Option();
opt2.OptionText = "Text2";
lstoption1.add(opt2)
Option opt3 = new Option();
opt3.OptionText = "Text3";
lstoption1.add(opt3)
List<Option> lstoption2 = new List<Option>();
Option opt = new Option();
opt.OptionText = "Text1";
lstoption2.add(opt)
Option opt2 = new Option();
opt2.OptionText = "Text4";
lstoption2.add(opt2)
Option opt3 = new Option();
opt3.OptionText = "Text3";
opt3.OptionChecked = false;
lstoptions2.add(opt3)
I need to compare theese two lists based on OptionText
1) var sameentities= which entities exisits in both lists.
ie : it should return entities that has OptionText as
**Text1 & Text3**
2) var existinlst1= which entities exists only in lstoptions1.
ie : it should return entity that has OptionText as
**Text2**
3) var notexistinlist1=which entities not exists only in lstoptions1.
ie : it should return entity that has OptionText as
**Text4**
I am using so many for loops for this but i want to do it in a simple way using linq.
is there any easy way to find them using LINQ in silverlight.
Thanks for any help.
Assuming equality is set up appropriately:
(I think – your description isn’t very clear.)
You can pass a custom equality comparer into all of these methods to compare just by option text if you need to. Note that if you only need the option text from the results, you can project it first: