如何解析字节列表的disctinc值? [英] How can disctinc values of list of byte?

查看:130
本文介绍了如何解析字节列表的disctinc值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dim Bytes As Byte() = {22, 123, 1, 1, 0, 22}





我想要订单和disticnt Bytes数组。



I want order and disticnt Bytes array.

Bytes = {0,1,22,123}





我该怎么办?

谢谢



我的尝试:





How can i do?
Thanks

What I have tried:

Dim Bytes As Byte() = {22, 123, 1, 1, 0, 22}
Dim ListByte As List(Of Byte) = Bytes.ToList
ListByte = ListByte.OrderBy(Function(x) x).ToList.Distinct

推荐答案

这可以做得像这样一个小清洁:

This can be done a little cleaner like this:
Dim bytes As Byte() = {22, 123, 1, 1, 0, 22}
bytes = bytes.Distinct().OrderBy(Function(x) x).ToArray()

可以在任何IEnumerable上调用Distinct,这是一个Byte数组。在您的情况下,Distinct()将返回IEnumerable(Of Byte)。由于您没有对多少元素进行排序,因此可以降低下一个操作成本。



也可以调用对OrderBy()的调用任何IEnumerable,即Distinct调用刚刚返回。它返回一个IOrderedEnumerable(Of Byte),它是IEnumerable的子类。这意味着你可以将任何IOrderedEnumerable视为IEnumerable一样,这就是你接下来要做的事情。



最后一个操作很简单。它将OrderBy的结果转换回数组。通过调用ToArray可以轻松完成。它从任何IEnumerable中创建一个新数组。



完成。你已经得到了你排序的,不同的数组。

Distinct can be called on any IEnumerable, which an array of Byte is. In your case, Distinct() will return an IEnumerable(Of Byte). This has the effect of making the next operation cost a little less since you're not sorting as many elements.

The call to OrderBy() can also be called on any IEnumerable, which the Distinct call just returned. It returns an IOrderedEnumerable(Of Byte), which is a subclass of IEnumerable. What that means is you can treat any IOrderedEnumerable the same as if it was an IEnumerable, which is what you're going to do next.

The last operation is simple. It converts the result of the OrderBy back to an array. That's easily done with the call to ToArray. It creates a new array out of any IEnumerable.

Done. YOu've got your sorted, distinct array.


你需要在最后添加ToList;不同的是返回一些其他类型的集合。



(使用var直到你弄清楚具体细节)。
You need to tack "ToList" on the end; Distinct is returning some other type of collection.

(Use "var" until you work out the specifics).


这篇关于如何解析字节列表的disctinc值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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