Linq to text选择最后

时间:2014-02-13 01:46:56

标签: vb.net linq

我需要找到特定字段的最后一次出现。我尝试了不同的方法,但无济于事。

这是我现在拥有的。它可以工作,但不能使用Select子句。 Select子句导致查询崩溃。

Dim Qry = From lines In File.ReadAllLines(curMonth)
          Let Data = lines.Split(" ")
          Let LogDate = Data(0)
          Let Time = Data(1)
          Let DataType = Data(2)
          Let DutyStatus = Data(3)
          Let Memo = lines.Split("/")
          Let Location = Memo(0)
          Let LocationTag = Memo(1)
          Let CoDriver = Memo(2)
          Let DriverNumber = Memo(3)
          Let Status = Memo(5)
          Where DataType = "35"

For Each lines In Qry
    Debug.Print(lines.LogDate & ", " & lines.Time & ", " & lines.DutyStatus & ", " & lines.DataType _
      & ", " & lines.LocationTag & ", " & lines.CoDriver & ", " & lines.Status)
Next

由于

这是一些额外的信息。 样本数据:

20130801 00:00 26 X /DRIVER/'Don Hall'/'1622'/290/'3.5.1.08'/
20130801 00:00 24 W /RULE//CAN2007/13.0/0.0/14.0/70.0/7.0/0/0/1/
20130801 00:00 31 C /POWER UNIT/'Tractor'/'8536'/1/''/'PY7341'/1374680419/''/''/''/''/''/''/''/''/0/''/'ON'/''/'Single'/
20130801 00:00 35 C /CODRIVER/'Margaret Hall'/'1623'/''/1/1365556442/
20130801 00:00 30 C /MOTOR CARRIER/'K-DAC Enterprises'/'3025 Sandhills RD., Baden, ON, N3A 3B8'/'3025 Sandhills RD., Baden, ON, N3A 3B8'/''/1/'1622'/'Don Hall'/1328079183/''/
20130802 00:00 03 X /MILES VEHICLE/94.0/1596267.0/1596361.0/2/
20130802 00:00 58 A /PREX/1//SCHED1/1//VCF/1//VMINOR/0//VMAJOR/0//POWERODOM/'1596267'//TOWEDODOM/'1596267'//TIME/'14:45'//LOCATION INSP/'Langley BC'/
20130802 00:00 04 X /MILES DRIVING/1596267.0/0.0/0.0/0.0/0.0/0.0/0.0/1596361.0/94.0/0/2/0.0/94.0/0.0/
20130802 00:00 25 X /PRINT/'20130803 01:23'/1/0/
20130802 12:45 32 X /TOWED UNIT/'Trailer'/'53566'/1/''/'K8601J'/1375453962/''/''/''/''/''/''/''/''/0/''/'ON'/''/'Single'/

此数据会在月末启动时生成另一个文件。 (每条记录都以日期开头。这个窗口正在包裹线条。)

linq查询所需的示例:

20131021, 11:30, X, 35, CODRIVER, 'Margaret Hall', 0

我希望这可以帮助你理解我的目标。

1 个答案:

答案 0 :(得分:0)

查看是否在DataType赋值帮助之后移动Where子句。在我看来,您的查询正在读取每一行并尝试按您希望的方式拆分它,而不仅仅是您想要解析的行。 Where子句应该将它们过滤掉。:

Dim Qry = From lines In File.ReadAllLines(curMonth)
          Let Data = lines.Split(" ")
          Let LogDate = Data(0)
          Let Time = Data(1)
          Let DataType = Data(2)
          Where DataType = "35"
          Let DutyStatus = Data(3)
          Let Memo = lines.Split("/")
          Let Location = Memo(0)
          Let LocationTag = Memo(1)
          Let CoDriver = Memo(2)
          Let DriverNumber = Memo(3)
          Let Status = Memo(5)
          Select LogDate & "," & Time & "," & DutyStatus & "," & DataType _
                 & "," & LocationTag  & "," & CoDriver & "," & Status

查询结果中的每个条目都有一个可以使用的格式化字符串。如果您需要集合而不是输出数据,您只需调用ToList方法。