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

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.

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.

can u suggest me how to integrate linq in my website

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"
         }
      }
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.