Linq Where Problem

I'm trying to use..

Dim subListItems = From items In listIn Where items.Surname.StartsWith("D")

Open in new window


but I get

A first chance exception of type 'System.NullReferenceException' occurred

If I remove the where clause there are no problems and I can see items with a surname starting with D.

What is going wrong ?
LVL 1
DodsworthAsked:
Who is Participating?
 
Jens FiedererConnect With a Mentor Test Developer/ValidatorCommented:
you could try
Where items.Surname isnot Nothing andalso items.surname.StartsWith("D")
0
 
Jens FiedererTest Developer/ValidatorCommented:
Probably one of the "items" has a null value for surname.
0
 
DodsworthAuthor Commented:
I don't want to 'ignore' null items though.  Ideally I'd like them to be grouped with items that have a zero length string as a surname.  Is that linq-able?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
jayakrishnabhCommented:
Dim abc As dynamic = From p In listIn Where p.Name IsNot Nothing Where p.Name.StartsWith("D")
0
 
Jens FiedererTest Developer/ValidatorCommented:
The whole POINT of using a where clause is to ignore items....originally you wanted to ignore all items where the surname did not start with "D".

If you are trying to do groupings you probably should be working with group ... by clauses.

You can adapt pretty much anything to LINQ....you can always write a function that takes an object and does arbitrary processing to do a yes/no (use/ignore) decision (a PREDICATE for a where clause) or that returns some useful value (for a group .. by clause).
0
 
Jens FiedererTest Developer/ValidatorCommented:
Some examples of using grouping operators are at


http://msdn.microsoft.com/en-us/vstudio/bb737908
0
All Courses

From novice to tech pro — start learning today.