.net 按ID为'的字符串对类列表进行排序;s

.net 按ID为'的字符串对类列表进行排序;s,.net,vb.net,list,sorting,.net,Vb.net,List,Sorting,我有一串逗号分隔的ID (通过一个SQL SP)我正在检索值以填充类“ProductInfo”的列表,该类有一个ID属性和一个Name属性 填充完列表后,我想根据原始字符串顺序按ID对列表进行排序 用于检索数据的SP按ID ASC排序,我无法更改SP Public Class ProductInfo Private _id as String Public Property ID as String ..get.. ..set.. End

我有一串逗号分隔的ID

(通过一个SQL SP)我正在检索值以填充类“ProductInfo”的列表,该类有一个ID属性和一个Name属性

填充完列表后,我想根据原始字符串顺序按ID对列表进行排序

用于检索数据的SP按ID ASC排序,我无法更改SP

Public Class ProductInfo
    Private _id as String
    Public Property ID as String
        ..get..
        ..set..
    End Property

    Private _name as String
    Public Property Name as String
        ..get..
        ..set..
    End Property
End Class


Dim strIds as String = "56312,73446,129873,49879,38979"

Dim Products As New List(Of ProductInfo)
Products = FillProductDetails(strIds)

Products.Sort(strIds) ''''Conceptual

这就是你用上帝级语言所做的

var sortedList = strIds.Select(x => Products.FirstOrDefault(y => y.ID == x));
我不熟悉你的野蛮剧本,但你可能会翻译

注意,最好使用ID数组,而不是逗号分隔的列表

var strIds = new[] {"56312", "73446", "129873", "49879", "38979", };
使其更易于在代码中使用

马克好心地为moonspeak提供了翻译:

Dim sortedList = strIds.Split(","c).Select(Function(x) Products.FirstOrDefault(Function(y) y.ID = x))
或者,以strIds作为数组

Dim sortedList = strIds.Select(Function(x) Products.FirstOrDefault(Function(y) y.ID = x))

这就是你用上帝级语言所做的

var sortedList = strIds.Select(x => Products.FirstOrDefault(y => y.ID == x));
我不熟悉你的野蛮剧本,但你可能会翻译

注意,最好使用ID数组,而不是逗号分隔的列表

var strIds = new[] {"56312", "73446", "129873", "49879", "38979", };
使其更易于在代码中使用

马克好心地为moonspeak提供了翻译:

Dim sortedList = strIds.Split(","c).Select(Function(x) Products.FirstOrDefault(Function(y) y.ID = x))
或者,以strIds作为数组

Dim sortedList = strIds.Select(Function(x) Products.FirstOrDefault(Function(y) y.ID = x))

这就是你用上帝级语言所做的

var sortedList = strIds.Select(x => Products.FirstOrDefault(y => y.ID == x));
我不熟悉你的野蛮剧本,但你可能会翻译

注意,最好使用ID数组,而不是逗号分隔的列表

var strIds = new[] {"56312", "73446", "129873", "49879", "38979", };
使其更易于在代码中使用

马克好心地为moonspeak提供了翻译:

Dim sortedList = strIds.Split(","c).Select(Function(x) Products.FirstOrDefault(Function(y) y.ID = x))
或者,以strIds作为数组

Dim sortedList = strIds.Select(Function(x) Products.FirstOrDefault(Function(y) y.ID = x))

这就是你用上帝级语言所做的

var sortedList = strIds.Select(x => Products.FirstOrDefault(y => y.ID == x));
我不熟悉你的野蛮剧本,但你可能会翻译

注意,最好使用ID数组,而不是逗号分隔的列表

var strIds = new[] {"56312", "73446", "129873", "49879", "38979", };
使其更易于在代码中使用

马克好心地为moonspeak提供了翻译:

Dim sortedList = strIds.Split(","c).Select(Function(x) Products.FirstOrDefault(Function(y) y.ID = x))
或者,以strIds作为数组

Dim sortedList = strIds.Select(Function(x) Products.FirstOrDefault(Function(y) y.ID = x))

根据您对
Products.Sort
的概念性使用,这里尝试使用重载,该重载需要
比较
委托

首先将ids字符串拆分为一个数组

Dim straIds As String() = strIds.Split(","c)
然后在数组中按位置排序。内联版本:

Products.Sort(Function(x, y) If(Array.IndexOf(straIds, x.Id) > Array.IndexOf(straIds, y.Id), -1, If(Array.IndexOf(straIds, x.Id) = Array.IndexOf(straIds, y.Id), 0, 1)))
    Products.Sort(Function(x, y) 
                      Dim i As Integer = Array.IndexOf(straIds, x.Id)
                      Dim j As Integer = Array.IndexOf(straIds, y.Id)
                      Return If(i > j, -1, If(i = j, 0, 1))
                  End Function)
或更可读的版本:

Products.Sort(Function(x, y) If(Array.IndexOf(straIds, x.Id) > Array.IndexOf(straIds, y.Id), -1, If(Array.IndexOf(straIds, x.Id) = Array.IndexOf(straIds, y.Id), 0, 1)))
    Products.Sort(Function(x, y) 
                      Dim i As Integer = Array.IndexOf(straIds, x.Id)
                      Dim j As Integer = Array.IndexOf(straIds, y.Id)
                      Return If(i > j, -1, If(i = j, 0, 1))
                  End Function)

不确定它是否能像写的那样工作,但有人试图提供一个
比较
委托,该委托将保留原始
strIds
字符串中的顺序

根据您对
产品.Sort的概念使用,这里尝试使用重载,该重载需要
比较
委托

首先将ids字符串拆分为一个数组

Dim straIds As String() = strIds.Split(","c)
然后在数组中按位置排序。内联版本:

Products.Sort(Function(x, y) If(Array.IndexOf(straIds, x.Id) > Array.IndexOf(straIds, y.Id), -1, If(Array.IndexOf(straIds, x.Id) = Array.IndexOf(straIds, y.Id), 0, 1)))
    Products.Sort(Function(x, y) 
                      Dim i As Integer = Array.IndexOf(straIds, x.Id)
                      Dim j As Integer = Array.IndexOf(straIds, y.Id)
                      Return If(i > j, -1, If(i = j, 0, 1))
                  End Function)
或更可读的版本:

Products.Sort(Function(x, y) If(Array.IndexOf(straIds, x.Id) > Array.IndexOf(straIds, y.Id), -1, If(Array.IndexOf(straIds, x.Id) = Array.IndexOf(straIds, y.Id), 0, 1)))
    Products.Sort(Function(x, y) 
                      Dim i As Integer = Array.IndexOf(straIds, x.Id)
                      Dim j As Integer = Array.IndexOf(straIds, y.Id)
                      Return If(i > j, -1, If(i = j, 0, 1))
                  End Function)

不确定它是否能像写的那样工作,但有人试图提供一个
比较
委托,该委托将保留原始
strIds
字符串中的顺序

根据您对
产品.Sort的概念使用,这里尝试使用重载,该重载需要
比较
委托

首先将ids字符串拆分为一个数组

Dim straIds As String() = strIds.Split(","c)
然后在数组中按位置排序。内联版本:

Products.Sort(Function(x, y) If(Array.IndexOf(straIds, x.Id) > Array.IndexOf(straIds, y.Id), -1, If(Array.IndexOf(straIds, x.Id) = Array.IndexOf(straIds, y.Id), 0, 1)))
    Products.Sort(Function(x, y) 
                      Dim i As Integer = Array.IndexOf(straIds, x.Id)
                      Dim j As Integer = Array.IndexOf(straIds, y.Id)
                      Return If(i > j, -1, If(i = j, 0, 1))
                  End Function)
或更可读的版本:

Products.Sort(Function(x, y) If(Array.IndexOf(straIds, x.Id) > Array.IndexOf(straIds, y.Id), -1, If(Array.IndexOf(straIds, x.Id) = Array.IndexOf(straIds, y.Id), 0, 1)))
    Products.Sort(Function(x, y) 
                      Dim i As Integer = Array.IndexOf(straIds, x.Id)
                      Dim j As Integer = Array.IndexOf(straIds, y.Id)
                      Return If(i > j, -1, If(i = j, 0, 1))
                  End Function)

不确定它是否能像写的那样工作,但有人试图提供一个
比较
委托,该委托将保留原始
strIds
字符串中的顺序

根据您对
产品.Sort的概念使用,这里尝试使用重载,该重载需要
比较
委托

首先将ids字符串拆分为一个数组

Dim straIds As String() = strIds.Split(","c)
然后在数组中按位置排序。内联版本:

Products.Sort(Function(x, y) If(Array.IndexOf(straIds, x.Id) > Array.IndexOf(straIds, y.Id), -1, If(Array.IndexOf(straIds, x.Id) = Array.IndexOf(straIds, y.Id), 0, 1)))
    Products.Sort(Function(x, y) 
                      Dim i As Integer = Array.IndexOf(straIds, x.Id)
                      Dim j As Integer = Array.IndexOf(straIds, y.Id)
                      Return If(i > j, -1, If(i = j, 0, 1))
                  End Function)
或更可读的版本:

Products.Sort(Function(x, y) If(Array.IndexOf(straIds, x.Id) > Array.IndexOf(straIds, y.Id), -1, If(Array.IndexOf(straIds, x.Id) = Array.IndexOf(straIds, y.Id), 0, 1)))
    Products.Sort(Function(x, y) 
                      Dim i As Integer = Array.IndexOf(straIds, x.Id)
                      Dim j As Integer = Array.IndexOf(straIds, y.Id)
                      Return If(i > j, -1, If(i = j, 0, 1))
                  End Function)

不确定它是否能像写的那样工作,但有人试图提供一个
比较
委托,该委托将保留原始
strIds
字符串中的顺序

我认为如果ProductInfo覆盖
.ToString
以返回ID,那么
Products.Sort()
就是您所需要的。否则,您可能需要提供一个提供排序智能的比较器:
sort(比较器作为System.Collections.Generic.IComparer(Of T))
您也可以使用
SortedList
。当然,问题是文本比较器会失败,比如
{“9011”,“100003”}
“9011”的排序高于“1000003”,这是一个IComparer可以处理/转换的。我认为如果ProductInfo覆盖
.ToString
以返回ID,那么
Products.sort()
就是您所需要的。否则,您可能需要提供一个提供排序智能的比较器:
sort(比较器作为System.Collections.Generic.IComparer(Of T))
您也可以使用
SortedList
。当然,问题是文本比较器会失败,比如
{“9011”,“100003”}
“9011”的排序高于“1000003”,这是一个IComparer可以处理/转换的。我认为如果ProductInfo覆盖
.ToString
以返回ID,那么
Products.sort()
就是您所需要的。否则,您可能需要提供一个提供排序智能的比较器:
sort(比较器作为System.Collections.Generic.IComparer(Of T))
您也可以使用
SortedList
。当然,问题是文本比较器会失败,比如
{“9011”,“100003”}
“9011”的排序高于“1000003”,这是一个IComparer可以处理/转换的。我认为如果ProductInfo覆盖
.ToString
以返回ID,那么
Products.sort()
就是您所需要的。否则,您可能需要提供一个提供排序智能的比较器:
sort(比较器作为System.Collections.Generic.IComparer(Of T))
您也可以使用
SortedList
。当然,问题是文本比较器将失败,出现类似
{“9011”,