Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# Linq with nullable types

Posted on 2016-08-12
3
Medium Priority
?
137 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 83

Expert Comment

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

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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.
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

610 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