5

Linq fails instead of returning null?

 3 years ago
source link: https://www.codesd.com/item/linq-fails-instead-of-returning-null.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Linq fails instead of returning null?

advertisements

I am trying to filter a list of items using the .Where method, and return the first item matching the filter.

However if there are no items matching the filter, instead of returning null it throws an exception.

Here is the line of code I am using:

DescendantNodes.Where(dNode => dNode.InnerText.Contains("rain")).First();

Is there a way to make this work except splitting to two instructions?

Thanks,

Teddy


You may also compress your statement thus:

DescendantNodes.FirstOrDefault(dNode => dNode.InnerText.Contains("rain"));

Tags linq

Related Articles

Return (null) instead of returning null - why parentheses?

I was looking at some MSDN Documentation on IXmlSerializable and the Example Code listed the following method: public XmlSchema GetSchema() { return(null); } Is there a semantic difference between return null and return (null), ie does it invoke some

Linq to sql constantly returns null

I have the following SQL query: SELECT DISTINCT Participant.BackgroundTrainingID, Location.TrainingSite FROM Registration, ProgramLocation, Participant, Program, Location WHERE ProgramLocation.LocationID = Location.LocationID AND ProgramLocation.Prog

Return the java string instead of returning null

I wrote the method 'charCount()' to return String 'chc', but the netbean is forcing me to return null. Does the 'return null' also debarring me use the 'chc' outside this method,or it is well returned. Being new to java I am confused. // static Strin

Java Spring 4 if validation failed, instead of returning the form, it simply prints the name of the view

I am from PHP world and trying to learn Java Spring framework in spare time. I was doing good until I decided to add validation to a form. Below is my code. package com.invokedynamic.spring; import java.text.DateFormat; import java.util.Date; import

NSNull returns & lt; null & gt; instead of returning null

I'm using the following code to check if a NSNumber has nil value. So I'm converting the NSNumber to string and Im checking if its length is 0. If it is of zero, Im returning NSNull else Im returning the number itself. - (id)NSNullToNilForKey:(NSNumb

Linq to XML query returns null

I have the following XML file: <?xml version="1.0" encoding="utf-8"?> <tmx version="1.4"> <header/> <body> <tu> <tuv lang="en"> <seg>Hello?</seg> </tuv> <tuv

Linq grouping .include ('Table') returns null on the table

I have a linq query that is grouping by answers by QuestionGroup. I need to have the table AssessmentQuestionsReference load so that i can bind to it in my WPF app. var groupedAnswers = from a in App.ents.AssessmentAnswers.Include("AssessmentQuestion

How do I return null for a json property instead of & ldquo; Data & rdquo;: []

I have decorated my property as [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public IEnumerable<Season> seasons { get; set; } The problem arises with the following code which returns "seasons": [] if no records exist. from

Scala: overwriting a generic Java method that returns null

I need to override the following Java method in a Scala class: public class Test<T> { public T test(T o, boolean x) { if (x) { return o; } return null; } } With the following approach (in Scala), the compiler complains, "Expression of type Null

When to raise an exception or return null?

I have a few functions on Data access layer public Order RetrieveById(int id) public List<Order> RetrieveByStatus(OrderStatus status) Now i am bit confuse on exception raising. In case of RetrieveById function, id which is less than 1 is an invalid

mockito stubbing returns null

I am using mockito as mocking framework. I have a scenerio here, my when(abc.method()).thenReturn(value) does not return value, instead it returns null. Here is how my class and test looks like. public class foo(){ public boolean method(String userNa

How to return NULL for a dual function with the Intel C compiler?

I have some code that I am porting from an SGI system using the MIPS compiler. It has functions that are declared to have double return type. If the function can't find the right double, those functions return "NULL" The intel C compiler does no

The peekService call from BroadcastReceiver.onReceive returns null if I close the activity

The problem I wrote a program for Android 4.3, with a main activity, a broadcast receiver and a service. The activity binds to the service in its onCreate method. The activity has a button that schedules an alarm 10 seconds in the future. The alarm t

Json RESTful Web Service replies return null in Java

I'm new to Android and Web Services programming using Java and I've been having issues connecting my android app to my java web service. I've deployed the web service on tomcat and it's connecting on my phone browser but the HttpClient returns null e

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK