如何在C#中区分两个列表? [英] How to get difference between two list in C#?
本文介绍了如何在C#中区分两个列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我有两个列表我希望在第三个列表中获得两个列表的区别。 我的第一个清单null null。
我尝试过:
列表< long?> RateCardList = new List< long?>();
列表< long> categorylist = new List< long>();
列表< long?> ThirdList = new List< long?>();
ThirdList = RateCardList.Except(categorylist);
我在ThirdList中遇到错误
解决方案
你得到错误,因为你的参数除了
有long
值,而不是RateListCard,持有long?
s。
这样做:
ThirdList = RateCardList.Except(categoryList.Select(x = > new long ?(x)));
.Select(x => new long?(x))
获取所有元素,调用代码使它们long?
s并返回一个新的IEnumerable< long?>
,您可以有效地传递给。
Hi All
I have two List I wanted to get difference of two list in 3rd list. my 1st list null able.
What I have tried:
List<long?> RateCardList = new List<long?>(); List<long> categorylist = new List<long>();
List<long?> ThirdList = new List<long?>();
ThirdList = RateCardList.Except(categorylist);
I am getting error in ThirdList
解决方案
You get the error because your argument toExcept
haslong
values, as opposed to RateListCard, that holdslong?
s.
Do this:
ThirdList = RateCardList.Except(categoryList.Select(x => new long?(x)));
.Select(x => new long?(x))
takes all elements, calls the code to make themlong?
s and returns a newIEnumerable<long?>
, which you can validly pass toExcept
.
这篇关于如何在C#中区分两个列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文