Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Linq Where Problem

Posted on 2014-04-07
6
194 Views
Last Modified: 2016-02-15
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 ?
0
Comment
Question by:Dodsworth
  • 4
6 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 39983137
Probably one of the "items" has a null value for surname.
0
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 500 total points
ID: 39983146
you could try
Where items.Surname isnot Nothing andalso items.surname.StartsWith("D")
0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39983198
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 5

Expert Comment

by:jayakrishnabh
ID: 39983278
Dim abc As dynamic = From p In listIn Where p.Name IsNot Nothing Where p.Name.StartsWith("D")
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 39983293
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
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 39983296
Some examples of using grouping operators are at


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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question