hous
hous

Reputation: 2679

Search works on all fields except the date field

I'd like to search data by date with format (dd/MM/yyyy) for example "20/01/2021". the search works on ADDRESS and NAME fields but not on the CREATE_DATE.

This is my code :

       // search
        if (!string.IsNullOrEmpty(search))
        {
            List<string> terms = search.Split(',').ToList().ConvertAll(d => d.ToLower());

            res = res.Where(x =>
            terms.Any(
                str => x.NAME.Contains(str) ||
                x.ADDRESS.Contains(str) ||  
                x.DATE_CREATE.ToString().Contains(str)
            
            ));

            var test = res.FirstOrDefault().DATE_CREATE.ToString();
        }

This is the request :

http://localhost:6289/api/Customer?search=20/01/2021,hous

and this is the outputs of terms and test var :

enter image description here

enter image description here

and this is how the dates are saved , they dates are with datetime type

enter image description here

Upvotes: 0

Views: 67

Answers (2)

Soul
Soul

Reputation: 121

Your code actually works on my machine BUT only in case I use appropriate culture settings as @JustShadow pointed out. Appropriate culture: which results a datetime.ToString() formating date in dd/MM/yyyy format. For test purpose I used "es-ES" culture for example with the following code inside your if statement:

Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ES");

I suggest modifying your code according to this:

if (!string.IsNullOrEmpty(search))
{
   List<string> terms = search.Split(',').ToList().ConvertAll(d => d.ToLower());

   var originalCulture = Thread.CurrentThread.CurrentCulture;
   try
   {
      // use any locale which results a datetime.ToString() output in dd/MM/yyyy format
      Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ES"); ;

      res = res.Where(x =>
      terms.Any(
         str => x.NAME.Contains(str) ||
         x.ADDRESS.Contains(str) ||
         x.DATE_CREATE.ToString().Contains(str)));

      var test = res.FirstOrDefault().DATE_CREATE.ToString();
   }
   finally
   {
      Thread.CurrentThread.CurrentCulture = originalCulture;
   }
}

This will be enough if your input will have always dd/MM/yyyy format.

Edit: You can try to use custom format string in ToString() call to achieve a working code as @bitapinches suggests. I think my solution performs better because there is no need to parse the custom format string in each comparison LINQ will execute. Here is the alternative for reference:

if (!string.IsNullOrEmpty(search))
{
   List<string> terms = search.Split(',').ToList().ConvertAll(d => d.ToLower());

   res = res.Where(x =>
      terms.Any(
      str => x.NAME.Contains(str) ||
      x.ADDRESS.Contains(str) ||
      x.DATE_CREATE.ToString(@"dd\/MM\/yyyy").Contains(str)));

   var test = res.FirstOrDefault().DATE_CREATE.ToString();
}

Upvotes: 2

bita pinches
bita pinches

Reputation: 63

I think cast datatime to date ToString() you need to convert date format like "dd/MM/yyyy" which is the parameter (search=20/01/2021) in your url then Here [x.DATE_CREATE.ToString().Contains(str)] i would use "equals" be like [x.DATE_CREATE.ToString("dd/MM/yyyy").equals(str)] to be more specific on the query.

Upvotes: 2

Related Questions