Solved

Linq Where Problem

Posted on 2014-04-07
6
192 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now