0

Hi All,

I am trying to use to linq to retrieve some data, and creating my queries dynamically with where clauses added to an IQueryable object.

The problem I am running into is filtering for days of the week. In the table, I have a datetime of a call. I want to be able to pick certain days of the week to filter for. I have those days of the week stored as strings in a string array. I was then trying to do a contains statement to see if the day of the week, based on the date time is one of those in the array.

My code is below

matches = matches.Where(CallRequest => CallRequest.dTimestamp != null && f.PrintDays.Contains(Convert.ToDateTime(CallRequest.dTimestamp).DayOfWeek.ToString()));

f.PrintDays is the string[], with the week days stored as "Sunday","Monday" etc.

I use the Convert statement, since it is a ?DateTime in the table, but by that point I have already verified that it is not null.

If anyone has any ideas, or maybe this isn't even possible, let me know.

Thanks!

Mark

4
Contributors
4
Replies
6
Views
6 Years
Discussion Span
Last Post by thines01
0

So what is the issue you are having? I created a class with a nullable DateTime, made an array of that class. Then I created an array of Strings with days of the week in them and did the code you have an had no issues.

0

Hmm, strange. The filter never seems to be applied to my data. I will have to look further into it. Thanks for taking the time. I know at least that it is possible. Not sure what is happening but I know at least that its not the String[].contains. Thanks again.

0

can u suggest me how to integrate linq in my website

Edited by rajees: n/a

0

Even though the thread is old...

using System;
using System.Linq;

namespace Dani_312188
{
   class CDani_312188
   {
      static void Main(string[] args)
      {
         string[] arr_strDays = {"Tuesday", "Thursday", "Saturday" };

         DateTime? dt1 = null;
         
         int intRnd = (new Random(DateTime.Now.Second)).Next();

         if (0.Equals(intRnd % 2)) dt1 = DateTime.Parse("18-June-2011");

         bool blnIsMatch =
         (
            null != dt1 //...must check for null
            && //...must cast to a regular DateTime
            arr_strDays.Contains(((DateTime)dt1).DayOfWeek.ToString())
         );

         if (blnIsMatch)
         {
            Console.WriteLine(((DateTime)dt1).DayOfWeek); // Prints "Saturday"
         }
      }
   }
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.