?
Solved

C# Linq with nullable types

Posted on 2016-08-12
3
Medium Priority
?
118 Views
Last Modified: 2016-08-15
I have a linq query which is pulling data into my object model.  Problem is that I'm getting an error because when I convert it it is an empty string.  How can I convert it to a null if it is an empty string?
var result = (from c in xdoc.Descendents("key")                      
                      select new MyObject()
                      {
                            field1 = DateTime.Pares( c.Element{"valuefromxml").value) )
                      });

Open in new window

I tried to cast it with (DateTIme?) instead but stil have an error.
0
Comment
Question by:CipherIS
[X]
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
3 Comments
 
LVL 10

Expert Comment

by:Jini Jose
ID: 41753617
where you want to get the null value ?
in field1 or in result ?
0
 
LVL 82

Expert Comment

by:David Johnson, CD, MVP
ID: 41753723
DateTime.Pares Should read DateTime.Parse
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 41753736
Replace this line of the code
field1 = DateTime.Pares( c.Element("valuefromxml").Value) )

Open in new window

To this
field1 = (c.Element("valuefromxml").Value != string.Empty) ? DateTime.Parse( c.Element("valuefromxml").Value) : (DateTime?)null

Open in new window

0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

801 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