LINQ query convert a variable to datetime ddMMyyyy

Joe 41 Reputation points
2022-06-28T18:50:17.937+00:00

Hi, how do I convert this variable x.DateCreated to a datetime format of ddMMyyyy?

 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>();  
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,250 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,471 Reputation points Microsoft Vendor
    2022-06-30T02:11:02.463+00:00

    Hi @Joe ,
    You can use the Enumerable.Select method to project each element of the sequence into a new form.
    For details, you can check the documentation.https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.select?view=net-6.0
    You could also try parsing locally instead of in the database via AsEnumerable.

    var query = db.tb1.Select(tb => tb.dt)  
                      .AsEnumerable() // Do the rest of the processing locally  
                      .Select(x => DateTime.ParseExact(x, "ddMMyyyy",  
                                                    CultureInfo.InvariantCulture));  
    

    If you just want to show it on the view/UI, you can use this:

    @model.DateCreated.ToString("dd / MM / yyyy")  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,366 Reputation points
    2022-06-29T01:50:18.443+00:00

    Use the linq select to map the row to a class of you own definition.

    0 comments No comments