VB.NET LINQ 操作数组代码(求交集,并集,差集,最值,平均,去重复)

VB.NET LINQ 操作数组代码(求交集,并集,差集,最值,平均,去重复)

对于两个数组的交集、并集、差集、平均、去重复的代码

	Dim a As Integer() = {
    1, 2, 3, 4, 5, 6, 7}
        Dim b As Integer() = {
    4, 5, 6, 7, 8, 9, 10}
        Dim c As Integer() = {
    1, 2, 3, 3, 4, 1, 2, 4, 6, 1, 6, 5}
        ' 交集 
        Dim Intersection_of_Sets() as integer = a.Intersect(b).toArray
        ' 并集 
        Dim union_of_Sets() as integer= a.Union(b).toArray
        ' a有b没有的 
        Dim diff1() as integer  = a.Except(b).toArray
        ' b有a没有的 
        Dim diff2() as integer = b.Except(a).toArray
        '最大
        Dim max = a.Max()
        '最小
        Dim min = a.Min() 
        Dim avg = a.Average() 
        '去重复
        Dim dis() as integer = c.Distinct().toArray

同过调用 Linq 使两个数组的操作变得简便。

参考:

https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/concepts/linq/how-to-find-the-set-difference-between-two-lists-linq
https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.intersect?redirectedfrom=MSDN&view=netframework-4.8#overloads

你可能感兴趣的:(VSTO)