Solved

C# Linq with nullable types

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

Expert Comment

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

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

695 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