LINQ.Startswith or. Fixes problems in VB.NET4

This may be a question for beginners ...

In my code, I can easily use "where Obj.Feld = String", but using "where Obj.Feld.StartsWith (" a ")" does not work. See the following two functions:

    Public Function EntriesByFileName(ByRef Database() As Entry, ByVal Filename As _
  String) As IEnumerable(Of Entry)
        Dim Result As IEnumerable(Of Entry) = From EntryObject In Database _
        Where (EntryObject.FileName = Filename) Select EntryObject
        Return Result
    End Function

    Public Function EntriesLikeFileName(ByRef Database() As Entry, ByVal _
      Filename As String) As IEnumerable(Of Entry)
        Filename = Filename.ToLower
        Dim Result As IEnumerable(Of Entry) = From EntryObject In Database _
          Where EntryObject.FileName.StartsWith("a") Select EntryObject
        Return Result
    End Function

The first function (byFileName) works fine. The second function (LikeFileName) does not work. Using Startswith, I get "Object reference not set to instance of object." What am I doing wrong?

A database is an array of objects, a structure consisting of strings

+3
source share
4 answers

EntryObject.FileNamemaybe NULLthat's why it EntryObject.FileName.StartsWith(..)can cause NullReferenceException.

NULL

if EntryObject.FileName <> nothing AndAlso EntryObject.FileName.StartsWith(..) 

AndAlso , , , , , NullReferenceException.

+4

, . = null, - , StartsWith() .

+2

EntryObject FileName, null. , , , .

+1

linq , , startswith contains. . fooobar.com/questions/141336/...

0
source

Source: https://habr.com/ru/post/1757782/


All Articles