LINQ query convert a variable to datetime ddMMyyyy

Joe

Member
Joined
Jun 22, 2022
Messages
7
Programming Experience
3-5
Hi, how do I convert this variable x.DateCreated to a datetime format of ddMMyyyy?

C#:
 tAccounts = tAccounts.Where(x => x.UserID.ToString().ToLower().Contains(searchValue.ToLower()) ||
                     x.EmailAddress.ToLower().Contains(searchValue.ToLower()) ||
                     x.DateCreated.ToString("ddMMyyyy").ToLower().Contains(searchValue.ToLower()) |         
                     ).ToList<Account>();
 
Solution
Why is DateCreated nullable in the first place? It seems like every existent record must have been created, so it seems like it should be non-nullable. If it's nullable because it actually can be null then just getting the Value property will throw an exception in cases where it is. You need to allow for both, which is exactly what null propagation allows you to do:
C#:
x.DateCreated?.ToString("ddMMyyyy").Contains(searchValue) == true
Obviously it's pointless calling ToLower when the text you're comparing to will only contain numeric digits. Note that that code is effectively the same as this:
C#:
x.DateCreated.HasValue && x.DateCreated.Value.ToString("ddMMyyyy").Contains(searchValue)

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,469
Location
Chesapeake, VA
Programming Experience
10+
What's wrong with your current call to ToString()? Is it giving you an error? If so, what is the error message?
 

Joe

Member
Joined
Jun 22, 2022
Messages
7
Programming Experience
3-5
I receive the following error..
Returns the text representation of the value of the current Nullable<T> object.
No overload for method ‘ToString’ takes 1 arguments
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,469
Location
Chesapeake, VA
Programming Experience
10+
What is the type of DateCreated?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,469
Location
Chesapeake, VA
Programming Experience
10+
Also, are you sure that ToLower() will cover all the cases? Beware of the Turkish I problem.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,469
Location
Chesapeake, VA
Programming Experience
10+
Are you saying that DateCreated is a list? (e.g. Nullable<List<DateTime>> DataCreated

Anyway if DateCreated is a Nullable<DateTime> DateCreated, then you need to access the Value property of Nullable<T>. See the .NET Fiddle.

C#:
using System;

class Foo
{
    public Nullable<DateTime> DateCreated { get; set; }
    
    public Foo()
    {
        DateCreated = DateTime.Now;
    }
}

class Program
{
    public static void Main()
    {
        var foo = new Foo();
        var formatted = foo.DateCreated.Value.ToString("ddMMyyyy");
        Console.WriteLine(formatted);
    }
}
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,469
Location
Chesapeake, VA
Programming Experience
10+
how do you access the list and format a date field before returning it to the view?
It's the view's job to format the date. Not the controller/presenter. Not the model. Not the view model.

The view should get a DateTime, and it would be the view's job to translate that internal DateTime into a presentable value be it a localized string, a clock graphic, a sun/moon representation, etc.
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,554
Location
Sydney, Australia
Programming Experience
10+
Why is DateCreated nullable in the first place? It seems like every existent record must have been created, so it seems like it should be non-nullable. If it's nullable because it actually can be null then just getting the Value property will throw an exception in cases where it is. You need to allow for both, which is exactly what null propagation allows you to do:
C#:
x.DateCreated?.ToString("ddMMyyyy").Contains(searchValue) == true
Obviously it's pointless calling ToLower when the text you're comparing to will only contain numeric digits. Note that that code is effectively the same as this:
C#:
x.DateCreated.HasValue && x.DateCreated.Value.ToString("ddMMyyyy").Contains(searchValue)
 
Solution
Top Bottom