2

I have two property for readonlucollection and IList and created another property of List<string> OptionList is readonlycollection<string> and ForList is IList<string> But I don't get full list in combine list it skipped some elements from optionlist.

How do i get full list?

public List<string> CombineList {
    get {
        return OptionList.Union(ForList).ToList();
    }
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
R.Shah
  • 23
  • 3

3 Answers3

2

Union method skips elements common to both lists. If you would like to have a list that keeps duplicates, use concatenation instead of union:

return OptionList.Concat(ForList).ToList();
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

Use Concat if you want to concatenate. Union has an implicit distinct built-in.

usr
  • 168,620
  • 35
  • 240
  • 369
0

The problem might be Unionand that you dont use Concat. There is a slight diference which is described in the post below: (hope this helpes :) )

In this post: Union Vs Concat in Linq

i would recomend this approach:

OptionList.Concat(forList).ToList();
Community
  • 1
  • 1