开发者

Can i Union 3 ToSelectList

I have three seperate ToSelectList items and i wanted to开发者_如何转开发 combine each list into one dropdown list box and was wondering if i could use a Union for it. Or is it that a union is only for 2 toselectlist items only.

Thanks!


When item exists in more than one list, do you want item to appear once, or as many times as they exists in all lists?

As I understand you want just concat:

var combinedList = list1.Concat(list2).Concat(list3).ToList();

If you want to avoid duplicates:

var unionList = list1.Union(list2).Union(list3).ToList();

Union is more expensive, as it has to go through the list and take care of duplicates. If your item is a reference type and there is no IComparable or IEquable interfaces and you don't provide IEqualityComparer, you likely don't need Union.


You can only union two lists at a time using Union() - but you can chain it to achieve what you want:

var resultList = list1.Union(list2).Union(list3).ToList();


You can try using the Union() linq extension method.

var finalSelectList = model.getFirstList().ToSelectList().Union(
                model.getSecondList().ToSelectList().Union(
                model.getThirdList().ToSelectList()))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜