将列表分成几部分-VB转换失败 [英] Split a List into parts - VB conversion fails

查看:89
本文介绍了将列表分成几部分-VB转换失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试编写将列表拆分为子列表的方法。

 私有函数SplitIdsIntoChunks(ByVal键作为List(Of String) ))As List(Of List(Of String))
返回键_
.Select(Function(x,i)New with {Key .Index = i,Key .Value = x})_
.GroupBy(Function(x)(x.Index / 10000))_
.Select(Function(x)x.Select(Function(v)v.Value).ToList())_
.ToList()
结束函数

我从此处
C#解决方案可以很好地工作。



我用VB编写的版本会返回带有一个元素而不是10000的列表集合。
我在哪里出错了? ?



预先感谢。



编辑1:



用法:

 将昏暗的块作为List(Of List(Of String))= SplitIdsIntoChunks (键)

键的内容:





我的方法返回列表列表中有一个项目:





预期结果:两个列表的列表-第一列为10000个项目,第二列为6256。

解决方案

主要的问题是在c#中,将一个整数除以一个整数 x.Index / 3 会得出一个整数。 VB中的标准除法 / 始终导致 Double ,因此每个项目都有一个唯一的组。例如:{.33,.333333,.333334}等。



技巧是使用整数除法 \ 获得整数结果。我还添加了 chunksize 参数以使其更加灵活和可重复使用:

 私有函数SplitIntoChunks(键作为列表(字符串),chunkSize作为整数)作为列表(列表(字符串))
返回键。
Select(Function(x,i)New with {Key .Index = i,Key .Value = x})。
GroupBy(Function(x)(x.Index \ chunkSize))。
Select(Function(x)x.Select(Function(v)v.Value).ToList())。
ToList()
结束函数

另一个区别是 c#在行继续方面非常灵活,因此点可以位于行的开头或结尾。 VB(自VS2010起)在许多内容(包括成员限定符(点))之后都支持隐式行连续 ,因此您可以摆脱显式的行连续性( _ )。



用法:

 '将1000个列表拆分为300个块
Dim splits = SplitIntoChunks(myList,300)

For n As Integer = 0拆分.Count-1
Console.WriteLine(拆分{0}有{1}个项目,n.ToString(),拆分(n)。计数)
下一个

输出/结果:


拆分0有300个项目

分组1有300个项目

分组2有300个项目

分组3有100个项目


请注意,在链接的C#帖子中有很多关于各种方法性能的评论。这样就可以正常工作,根据实际拆分的内容,某些替代方法可能会表现更好。 跳过获取这里使用的方法也很有趣。


Trying to write a method that splits a list into sublists.

Private Function SplitIdsIntoChunks(ByVal keys As List(Of String)) As List(Of List(Of String))
    Return keys _
        .Select(Function(x, i) New With {Key .Index = i, Key .Value = x}) _
        .GroupBy(Function(x) (x.Index / 10000)) _
        .Select(Function(x) x.Select(Function(v) v.Value).ToList()) _
        .ToList()
End Function

I used C# solution from here. C# solution works fine.

My version written in VB returns a collection of lists with one element instead of 10000. Where did I go wrong?

Thanks in advance.

Edit 1:

Usage:

Dim chunks As List(Of List(Of String)) = SplitIdsIntoChunks(keys)

'Keys' content:

My method returns a lists of lists with one item inside:

Expected result: list of two lists - 10000 items in first and 6256 in second.

解决方案

The main 'problem' is that in c# dividing an int by an int, x.Index / 3, results in an integer. Standard division / in VB always results in a Double and therefore a unique Group for each item. e.g.: {.33, .333333, .333334} etc.

The 'trick' is to use integer division \ to get an integer result. I also added a chunksize argument to make it more flexible and reusable:

Private Function SplitIntoChunks(keys As List(Of String), chunkSize As Integer) As List(Of List(Of String))
    Return keys.
            Select(Function(x, i) New With {Key .Index = i, Key .Value = x}).
            GroupBy(Function(x) (x.Index \ chunkSize)).
            Select(Function(x) x.Select(Function(v) v.Value).ToList()).
            ToList()
End Function

One other difference is that c# is very flexible regarding line continuation, so the dot can be at the start or end of a line. VB (since VS2010) supports implicit line continuation after a great many things including member qualifier character (the dot .) so you can get rid of the explicit line continuation (" _").

Usage:

' split list of 1000 into 300 chunks
Dim splits = SplitIntoChunks(myList, 300)

For n As Integer = 0 To splits.Count - 1
    Console.WriteLine("Split {0} has {1} items", n.ToString(), splits(n).Count)
Next

Output/Result:

Split 0 has 300 items
Split 1 has 300 items
Split 2 has 300 items
Splits 3 has 100 items

Note that there is a lot of commentary on the linked C# post about performance of various methods. This just gets it working, some of the alternatives might perform better depending on what is actually being split. The Skip and Take approach used here is interesting too.

这篇关于将列表分成几部分-VB转换失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆