【问题标题】:Reading two column data into a list, sorting alphabetically将两列数据读入列表,按字母顺序排序
【发布时间】:2017-03-16 03:11:58
【问题描述】:

我需要导入一份额外清单(随餐提供的酱汁)及其各自的价格。使用老师的一些代码,我可以读取文件,将额外的名称和价格放入列表中,然后显示它,表明它已经工作了。现在问题来了,当我必须按字母顺序对附加内容进行排序时,我才能对其应用二进制搜索。我已经用 .sort 和 OrderBy 等尝试了一切,但没有任何效果。

我的包含数据的外部文本文件

鹰嘴豆泥,0.75
辣椒,0.50
塔布利,1.25
Tzatziki,0.50

额外的公共课

Public Class extra
    Public Property Name As String
    Public Property Price As Decimal
End Class

此代码导入数据并将其放入列表中

Dim allExtras = From line In System.IO.File.ReadLines("C:\Users\ExtrasList.txt")
                Let Columns = line.Split(","c)
                Where Columns.Length = 2
                Let Price = Decimal.Parse(Columns(1).Trim())
                Let Name = Columns(0).Trim()
                Select New extra With {.Name = Name, .Price = Price}

Dim extraList As List(Of extra) = allExtras.ToList()

显示列表

Console.WriteLine()
Console.WriteLine("Extra Name" & vbTab & "Price")
Console.WriteLine("-----------" & vbTab & "-----")
For Each extra In extraList
    Console.WriteLine(extra.Name & vbTab & vbTab & extra.Price)
Next

我只需要将额外文件及其价格从单独的文件中获取到可以按字母顺序排序的数组或列表中,以便用户可以搜索以查看是否存在他们想要的额外内容。我还需要能够访问列表,以便用户可以选择额外的,然后将所需的价格添加到总价中。任何帮助将不胜感激,因为这部分完全阻止了我的进一步发展。我不介意是否有人能够帮助我使用我现有的代码对列表进行排序,或者向我展示一种更有效的方法来将文件中的数据读取到列表中,我只需要某种解决方案。

【问题讨论】:

  • 在您的代码中,您没有尝试排序或OrderByOrderBy 应该可以工作。如果您尝试使用OrderBy,那么“它不起作用”是什么意思?

标签: .net vb.net visual-studio sorting search


【解决方案1】:
Imports System.Linq
. . . . . . 
' I think, this is cleaner way to load list
Dim eList as List(Of Extra) = File.ReadAllLines("C:\Users\ExtrasList.txt").
    Select(
        Function(l) 
            Dim a() As = l.Split(",".ToCharArray())
            Return New Extra With { .Name = a[0].Trim(), .Price = Decimal.Parse(a(1).Trim()) } 
         End Function).ToList()


......
' Check if extra exists. Optionally, you can use string comparison options
If (eList.Any(Function(i) String.Equals(i.Name, "The Name"))  Then

......
' get price of specific extra
Dim price As Decimal = eList.Single(Function(i) String.Equals(i.Name, "The Name")).Price

【讨论】:

    【解决方案2】:

    按名称搜索和按名称获取价格的最有效方法是将名称和对应价格发送到Dictionary

    Dim sauces = File.ReadAllLines("C:\Users\ExtrasList.txt").
                      Select(Function(line) line.Split(",")).
                      ToDictionary(Function(pair) pair(0), Function(pair) pair(1))
    

    那你就可以用了

    Dim price As String
    If sauces.TryGetValue("Hummus", price) = True Then
        ' Use price
    Else
        ' Show not found message
    End If
    

    对于列表,您可以遍历所有值-键对

    Dim orderedPairs = sauces.OrderBy(Function(pair) pair.Key)
    For Each sauce As KeyValuePair(Of String, String) In orderedPairs 
        Dim name = sauce.Key
        Dim price = sauce.Value
    Next  
    

    我建议为具有适当类型的酱汁创建自己的类

    Public Class Sauce
        Public Property Name As String
        Public Property Price As Decimal
    End Class
    

    然后你可以通过引入自己的方法来简化你的阅读方法,将line转换为Sauce的实例

    Public Function LineToSauce(line As String) As Sauce
        Dim pair As String() = line.Split(",")
        Return New Sauce With
        {
            .Name = pair(0),
            .Price = Decimal.Parse(pair(1))
        }
    End Function
    
    ' Reading
    Dim sauces = File.ReadAllLines("C:\Users\ExtrasList.txt").
                      Select(AddressOf LineToSauce).
                      ToDictionary(Function(sauce) sauce.Name)
    
    ' searching by name
    Dim sauce As Sauce
    If sauces.TryGetValue("Hummus", sauce) = True Then
        ' Use sauce.Price or sauce.Name
    Else
        ' Show not found message
    End If
    
    ' iterating and ordering
    Dim orderedSauces = sauces.Values.OrderBy(Function(sauce) sauce.Name)
    For Each sauce As orderedSauces
        Dim name = sauce.Name
        Dim price = sauce.Price
    Next  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 1970-01-01
      相关资源
      最近更新 更多