Solved

C# Linq with nullable types

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 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

13 Experts available now in Live!

Get 1:1 Help Now