Ok I was in the process of getting an answer for another SO question, and I came up with the following function to get a distinct list of int's:
static List<Int32> Example(params List<Int32> lsts)
{
List<Int32> result = new List<int>();
foreach (var lst in lsts)
{
result = result.Concat(lst).ToList();
}
return result.Distinct().OrderBy(c => c).ToList();
}
When I look at var
in VS2012, it says its of type Int32
not List<Int32>
. Shown here:
Shouldn't var be type List<Int32>
??
you are missing a []
at the end of the parameter type declaration:
// v-- this is missing in your code
static List<Int32> Example(params List<Int32>[] lsts)
{
List<Int32> result = new List<int>();
foreach (var lst in lsts)
{
result = result.Concat(lst).ToList();
}
return result.Distinct().OrderBy(c => c).ToList();
}
You're being misled by a different compiler error.
Your parameter is not an array.
You need to change the parameter to params List<Int32>[] lsts
to make it an array of lists. (or, better yet, params IEnumerable<Int32>[] lsts
)
Note that you can also get rid of the foreach
loop completely and write
return lsts.SelectMany(list => list)
.Distinct()
.OrderBy(i => i)
.ToList();
带有关键字params
必须是一个数组。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.