0

I must to retry a nullable Datetime from an Entity Framework and convert it in a Masked string of this type: ToString("dd-MM-yyyy")

I retry all the value with this code(now i show the only DataLead)

 var dbResult = conc.Clientes.Where(x => x.StatoCliente == "1").ToList();
                var leads = from e in dbResult
                            select new
                            { 
                              e.DataLead
                            } into selectedLead
                            select selectedLead;
                return Json(leads, JsonRequestBehavior.AllowGet);

In other part of my code i do the conversion with this code:

 DateTime date = (DateTime) DataLead;
 string dateElaborated = date.ToString("dd-MM-yyyy");

But, how can i do this operation in my LINQ selection function?

Thanks to all

4 Answers4

1

You can use string.Format (suppose e.DateLead is nullable DateTime type):

var dbResult = conc.Clientes.Where(x => x.StatoCliente == "1").ToList();
var leads = from e in dbResult
    select new
    {
        Datalead = string.Format("{0:dd-MM-yyyy}", e.DataLead)
    };

Or the new feature in C# 6

var leads = from e in dbResult
    select new
    {
        Datalead = e.DataLead?.Value.ToString("dd-MM-yyyy")
    };
Stephen Zeng
  • 2,748
  • 21
  • 18
0
var leads = from e in dbResult
            select new {
                selectedLead = e.DataLead != null ? e.DataLead.ToString("dd-MM-yyyy"): string.Empty
            }; 
oɔɯǝɹ
  • 7,219
  • 7
  • 58
  • 69
Hitesh Thakor
  • 471
  • 2
  • 12
  • You are missing the ? – RIanGillis Jul 22 '16 at 13:44
  • No, that won't compile. If not for any other reason as there is no such thing as `nulle`. – rory.ap Jul 22 '16 at 13:46
  • Also, the preferred way to check if a nullable is null is with `HasValue`. – rory.ap Jul 22 '16 at 13:47
  • I have try it but doesn't work. I have an error on ":" - it ask me the } – Roberto Mindoli Jul 22 '16 at 13:48
  • Actually scratch what I just said. Proof: http://stackoverflow.com/questions/676078/which-is-preferred-nullable-hasvalue-or-nullable-null – rory.ap Jul 22 '16 at 13:48
  • Although this code may be help to solve the problem, providing additional context regarding _why_ and/or _how_ it answers the question would significantly improve its long-term value. Please [edit] your answer to add some explanation. – oɔɯǝɹ Jul 22 '16 at 21:12
0

How about this:

// Get alternative text if DataLead is null
var leads = dbResult.Select(entry => entry.DataLead)
                    .Select(date => date.HasValue 
                                    ? date.Value.ToString("dd-MM-yyyy") 
                                    : "unknown");

// Get only entries where DataLead is not null.
var leads = dbResult.Select(entry => entry.DataLead)
                    .Where(entry => entry.HasValue)
                    .Select(date => date.Value.ToString("dd-MM-yyyy"));
Oliver
  • 43,366
  • 8
  • 94
  • 151
  • Thanks but i use Select New because i need to load a lot of parameters...for simplicity i have write only DataLead, but I have almost 20 dates to be loaded – Roberto Mindoli Jul 22 '16 at 13:51
0
var leads = conc.Clientes
    .Where(o => o.StatoCliente == "1" && o.DataLead.HasValue)
    .Select(o => o.DataLead.Value.ToString("dd-MM-yyyy"))
    .ToList();

Simple and clear. Also, I'd strongly advise to not use string literals like "1", but introduce an enum or a const value.

Phronux
  • 125
  • 1
  • 9
  • Thanks but i use Select New because i need to load a lot of parameters...for simplicity i have write only DataLead, but I have almost 20 dates to be loaded – Roberto Mindoli Jul 22 '16 at 13:50
  • So check all the dates and get their values. And try to be more specific next time. – Phronux Jul 22 '16 at 13:53