Solved

C# Linq with nullable types

Posted on 2016-08-12
3
61 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
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 78

Expert Comment

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

Accepted Solution

by:
Fernando Soto earned 500 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

12 Experts available now in Live!

Get 1:1 Help Now