在VB.Net中,使用Lambda(或LINQ)将集合拆分为n个部分

在VB.Net中,使用Lambda(或LINQ)将集合拆分为n个部分,vb.net,linq,lambda,Vb.net,Linq,Lambda,下面给出的代码示例运行良好,它正在做应该做的事情,但我并不满意。在VB.NET中寻找更智能的解决方案。结果的呈现(我指的是每个分组的计数)相当尴尬。数据内容和记录清单等并不重要。此外,计数应按小于{0}、从{1}到{2}、大于{3}的顺序排序…提前感谢 Dim Age1 As Integer = 5 Dim Age2 As Integer = 9 Dim myList As New List(Of Integer) = {1, 3, 5, 7, 9, 2, 4, 6, 8

下面给出的代码示例运行良好,它正在做应该做的事情,但我并不满意。在VB.NET中寻找更智能的解决方案。结果的呈现(我指的是每个分组的计数)相当尴尬。数据内容和记录清单等并不重要。此外,计数应按小于{0}、从{1}到{2}、大于{3}的顺序排序…提前感谢

    Dim Age1 As Integer = 5
    Dim Age2 As Integer = 9
    Dim myList As New List(Of Integer) = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 2, 4, 5, 7, 8, 9, 6, 7, 9, 11}
    Dim Lambda = myList.GroupBy(Function(x) New With {Key .Age1 = (x) < Age1,Key .Age2 = (x) > Age1 - 1 And (x) <= Age2,Key .Age3 = (x) > Age2}).ToList()
    Dim group1, group2, group3 As Integer
    myList = myList.OrderBy(Function(x) x).ToList()
    Console.WriteLine(String.Join(",", myList.Select(Function(s) s.ToString).ToArray))
    For Each group In Lambda
        If group.Key.Age1 Then group1 = group.Count()
        If group.Key.Age2 Then group2 = group.Count()
        If group.Key.Age3 Then group3 = group.Count()
    Next
    ' Obviously If Stop Then Error condition
    If group1 + group2 + group3 <> myList.Count Then Stop

    Console.WriteLine(String.Format("Groups: Less{0},From{1}To{2},MoreThan{3}", Age1, Age1, Age2 - 1, Age2))
    Console.WriteLine(String.Format("   Age:  {0,4},{1,8},{2,8}", group1, group2, group3))
    '1,2,2,3,4,4,5,5,6,6,7,7,7,8,8,9,9,9,10,11
    'Groups: Less5,From5To8,MoreThan9
    'Age:     6,      12,        2
Dim Age1为整数=5
尺寸2为整数=9
Dim myList作为新列表(整数)={1,3,5,7,9,2,4,6,8,10,2,4,5,7,8,9,6,7,9,11}
Dim Lambda=myList.GroupBy(函数(x)New,带有{Key.Age1=(x)Age1-1和(x)Age2})
将组1、组2、组3设置为整数
myList=myList.OrderBy(函数(x)x.ToList())
Console.WriteLine(String.Join(“,”,myList.Select(函数s.ToString.ToArray))
对于Lambda中的每个组
如果group.Key.Age1,则group1=group.Count()
如果group.Key.Age2,则group2=group.Count()
如果group.Key.Age3,则group3=group.Count()
下一个
'显然,如果停止,则为错误条件
如果group1+group2+group3是myList.Count,则停止
WriteLine(String.Format(“组:小于{0},从{1}到{2},大于{3}”,年龄1,年龄1,年龄2-1,年龄2))
WriteLine(String.Format(“年龄:{0,4},{1,8},{2,8}”,组1,组2,组3))
'1,2,2,3,4,4,5,5,6,6,7,7,7,8,8,9,9,9,10,11
“组:5岁以下,5岁至8岁,9岁以上
“年龄:6岁、12岁、2岁

以下是我将如何改进它:

Dim Age1 As Integer = 5
Dim Age2 As Integer = 9

Dim myList As New List(Of Integer) From {1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 2, 4, 5, 7, 8, 9, 6, 7, 9, 11}
Console.WriteLine(String.Join(", ", myList.OrderBy(Function(x) x)))
console.WriteLine

Dim ageBins = myList.GroupBy(Function(age) If(age < Age1, 1, If(age >= Age1 And age <= Age2, 2, 3))) _
                    .Select(Function(agebin) New With { agebin.Key, .AgeCount = agebin.Count() }) _
                    .OrderBy(Function(agebin) agebin.Key)

For Each bin In ageBins
    Dim msg As String
    Select bin.Key
        Case 1
            msg = $"Less {Age1}"
        Case 2
            msg = $"From {Age1} To {Age2}"
        Case Else
            msg = $"MoreThan {Age2}"
    End Select
    Console.WriteLine($"{msg,12}: {bin.AgeCount}")
Next
Dim Age1为整数=5
尺寸2为整数=9
Dim myList作为来自{1,3,5,7,9,2,4,6,8,10,2,4,5,7,8,9,6,7,9,11}的新列表(整数)
Console.WriteLine(String.Join(“,”,myList.OrderBy(函数(x)x)))
控制台写入线

Dim ageBins=myList.GroupBy(函数(年龄)If(年龄=Age1和年龄以下是我将如何改进它的方法:

Dim Age1 As Integer = 5
Dim Age2 As Integer = 9

Dim myList As New List(Of Integer) From {1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 2, 4, 5, 7, 8, 9, 6, 7, 9, 11}
Console.WriteLine(String.Join(", ", myList.OrderBy(Function(x) x)))
console.WriteLine

Dim ageBins = myList.GroupBy(Function(age) If(age < Age1, 1, If(age >= Age1 And age <= Age2, 2, 3))) _
                    .Select(Function(agebin) New With { agebin.Key, .AgeCount = agebin.Count() }) _
                    .OrderBy(Function(agebin) agebin.Key)

For Each bin In ageBins
    Dim msg As String
    Select bin.Key
        Case 1
            msg = $"Less {Age1}"
        Case 2
            msg = $"From {Age1} To {Age2}"
        Case Else
            msg = $"MoreThan {Age2}"
    End Select
    Console.WriteLine($"{msg,12}: {bin.AgeCount}")
Next
Dim Age1为整数=5
尺寸2为整数=9
Dim myList作为来自{1,3,5,7,9,2,4,6,8,10,2,4,5,7,8,9,6,7,9,11}的新列表(整数)
Console.WriteLine(String.Join(“,”,myList.OrderBy(函数(x)x)))
控制台写入线

Dim ageBins=myList.GroupBy(函数(年龄)If(年龄=Age1和年龄)在我看来,这是最简单的方法:

Dim Age1 As Integer = 5
Dim Age2 As Integer = 9
Dim myList As New List(Of Integer) From {1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 2, 4, 5, 7, 8, 9, 6, 7, 9, 11}

Dim group1 As Integer = myList.Where(Function (x) x < Age1).Count()
Dim group2 As Integer = myList.Where(Function (x) x > Age1 - 1 And x <= Age2).Count()
Dim group3 As Integer = myList.Where(Function (x) x > Age2).Count()

If group1 + group2 + group3 <> myList.Count Then Stop

Console.WriteLine(String.Format("Groups: Less{0},From{1}To{2},MoreThan{3}", Age1, Age1, Age2 - 1, Age2))
Console.WriteLine(String.Format("   Age:  {0,4},{1,8},{2,8}", group1, group2, group3))
Dim Age1为整数=5
尺寸2为整数=9
Dim myList作为来自{1,3,5,7,9,2,4,6,8,10,2,4,5,7,8,9,6,7,9,11}的新列表(整数)
整数形式的Dim group1=myList.Where(函数(x)xAge1-1和x Age2).Count()
如果group1+group2+group3是myList.Count,则停止
WriteLine(String.Format(“组:小于{0},从{1}到{2},大于{3}”,年龄1,年龄1,年龄2-1,年龄2))
WriteLine(String.Format(“年龄:{0,4},{1,8},{2,8}”,组1,组2,组3))

如果您想要一种基于LINQ的时髦方法,请尝试以下方法:

Dim bands() As Func(Of Integer, Boolean) = _
{ _
    Function (x) x < Age1, _
    Function (x) x <= Age2, _
    Function (x) True _
}

Dim counts = _
    myList _
        .GroupBy(Function (x) Enumerable.Range(0, bands.Count).Where(Function (n) bands(n)(x)).First()) _
        .Select(Function (x) x.Count()) _
        .ToArray()

Dim group1 As Integer = counts(0)
Dim group2 As Integer = counts(1)
Dim group3 As Integer = counts(2)
Dim bands()作为Func(整型,布尔型)=_
{ _
函数(x)x函数(x)x在我看来,这是最简单的方法:

Dim Age1 As Integer = 5
Dim Age2 As Integer = 9
Dim myList As New List(Of Integer) From {1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 2, 4, 5, 7, 8, 9, 6, 7, 9, 11}

Dim group1 As Integer = myList.Where(Function (x) x < Age1).Count()
Dim group2 As Integer = myList.Where(Function (x) x > Age1 - 1 And x <= Age2).Count()
Dim group3 As Integer = myList.Where(Function (x) x > Age2).Count()

If group1 + group2 + group3 <> myList.Count Then Stop

Console.WriteLine(String.Format("Groups: Less{0},From{1}To{2},MoreThan{3}", Age1, Age1, Age2 - 1, Age2))
Console.WriteLine(String.Format("   Age:  {0,4},{1,8},{2,8}", group1, group2, group3))
Dim Age1为整数=5
尺寸2为整数=9
Dim myList作为来自{1,3,5,7,9,2,4,6,8,10,2,4,5,7,8,9,6,7,9,11}的新列表(整数)
整数形式的Dim group1=myList.Where(函数(x)xAge1-1和x Age2).Count()
如果group1+group2+group3是myList.Count,则停止
WriteLine(String.Format(“组:小于{0},从{1}到{2},大于{3}”,年龄1,年龄1,年龄2-1,年龄2))
WriteLine(String.Format(“年龄:{0,4},{1,8},{2,8}”,组1,组2,组3))

如果您想要一种基于LINQ的时髦方法,请尝试以下方法:

Dim bands() As Func(Of Integer, Boolean) = _
{ _
    Function (x) x < Age1, _
    Function (x) x <= Age2, _
    Function (x) True _
}

Dim counts = _
    myList _
        .GroupBy(Function (x) Enumerable.Range(0, bands.Count).Where(Function (n) bands(n)(x)).First()) _
        .Select(Function (x) x.Count()) _
        .ToArray()

Dim group1 As Integer = counts(0)
Dim group2 As Integer = counts(1)
Dim group3 As Integer = counts(2)
Dim bands()作为Func(整型,布尔型)=_
{ _
函数(x)x函数(x)x这里是第二快的,我认为基于几小时前发布的@Enigmativity概念的相当“干净”的解决方案……注意了他的n波段方法

Function simpleCSVsplit(ageForBins() As Integer, myList As List(Of Integer)) As List(Of Integer)
    Dim Bands As New List(Of Integer)
    For indx As Integer = 0 To ageForBins.Count - 1
        Bands.Add(myList.Where(Function(x) x < ageForBins(indx)).Count())
        myList = myList.Skip(Bands(indx)).ToList()
    Next
    Bands.Add(myList.Count)
    Return Bands
End Function
函数simplecsvssplit(ageForBins()作为整数,myList作为列表(整数))作为列表(整数)
将标注栏设置为新列表(整数)
对于indx,整数=0到ageForBins.Count-1
Bands.Add(myList.Where(函数(x)x
这里是第二快的,我认为基于几小时前发布的@Enigmativity概念的相当“干净”的解决方案……照顾到了他的n波段方法

Function simpleCSVsplit(ageForBins() As Integer, myList As List(Of Integer)) As List(Of Integer)
    Dim Bands As New List(Of Integer)
    For indx As Integer = 0 To ageForBins.Count - 1
        Bands.Add(myList.Where(Function(x) x < ageForBins(indx)).Count())
        myList = myList.Skip(Bands(indx)).ToList()
    Next
    Bands.Add(myList.Count)
    Return Bands
End Function
函数simplecsvssplit(ageForBins()作为整数,myList作为列表(整数))作为列表(整数)
将标注栏设置为新列表(整数)
对于indx,整数=0到ageForBins.Count-1
Bands.Add(myList.Where(函数(x)x
计数、排序、分组……所有这些听起来都像是
DataTable
操作。也许这会让代码闻起来很好闻?@Jacob H……在我的例子中,使用“DataTable”是不可能的。我处理的是非常大的数据集,只有“List”是一种替代方法。groupby实际上做得很好……也不需要排序。之后对几个数字进行排序要比之前对数百万个数字进行排序快得多……一定有什么地方有更好的语法技巧……在C语言中,这样就不会那么冗长了;)但是我会看看我是否能翻译成VB。计数、排序、分组……所有听起来都像是
DataTable
操作。也许这会让代码闻起来很好闻?@Jacob H……在我的例子中,使用“DataTable”是不可能的。我处理的是非常大的数据集,只有“List”是一种替代方法。groupby实际上做得很好。…也不需要排序。排序之后的几个数字要比之前的数百万数字快得多…一定有什么诀窍