There are two lists

var buffList1 = new List<string>(){"1~2~3","13~23~123"}; var buffList2 = new List<string>(){"1~3~4","1~3~5"}; 

To select unique values ​​I do this.

 var rezList = buffList1.Except(buffList2).Union(buffList2.Except(buffList1)).ToList(); 

Interested in using the Except and Union methods for custom fields, i.e. Let's say there are 2 lines in the lists - "1 ~ 2 ~ 3" and "2 ~ 5 ~ 3". Split ('~') strings must be made and these two methods applied to values 2 and 5 .

Currently, Except and Union are working all along the line. How to override them?

  • one
    Break your lines into objects. Apply a custom IEqualityComparer . - VladD pm
  • @VladD, I already had a similar question - Grundy
  • I think that in any case you will have to do some preparation of lines for comparison. You can do it, as VlaD said, using a custom comparator, in other words, put your Split there. But in my opinion, this is not rational, as each time Split will be called for the collection with which you compare. It seems to me that it is easier to make a separate method. - iluxa1810
  • can be more? - asda

1 answer 1

If I correctly understood the task - to get a list of unique values, among the elements separated by the ~ sign, then the following should work:

 var res = buffList1 .SelectMany(s => s.Split("~")) .Union(buffList2 .SelectMany(s => s.Split("~"))) .Distinct();