Solved

Wher clause i a LINQ query

Posted on 2010-11-24
22
644 Views
Last Modified: 2013-12-17
Hi,
I have written the following query:
****************************************
var docs = (from doc in ctx.MyFiles
                            where (doc.Modified.Value.ToLongDateString().Equals(today))
                            select doc);
                foreach (var doc in docs)
                {
                    ModifiedFile modifedFile = new ModifiedFile();
                    modifedFile.FileName = doc.Name;
                    modifedFile.FileLocation = doc.Path;
                    modifedFile.DateLastModified = doc.Modified.Value;
                 }
****************************************
and I am getting the following error a the beginning of the foreach loop:
The expression ([10007].Modified.Value.ToLongDateString() == "24 November 2010") is not supported

What am I doing wrong??
0
Comment
Question by:racineconde
22 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 34205811
try

doc.Modified.Value.ToString("dd mmmm yyyy).Equals(today))
0
 
LVL 22

Expert Comment

by:p_davis
ID: 34205817
seems to be that today is a different date/string format than doc.Modified.Value.ToLongDateString().
0
 
LVL 22

Expert Comment

by:p_davis
ID: 34205822
or it might be better to parse it and have them both be DateTime objects
0
 

Author Comment

by:racineconde
ID: 34205879
I am getting the same kind of error. The message is:
The expression ([10007].Modified.Value.ToString("dd MMMM yyyy") == "24 November 2010") is not supported.

I tried both mmmm and MMMM for the month
0
 
LVL 22

Expert Comment

by:p_davis
ID: 34205890
ok, so what is the value for

Modified.Value ?
0
 

Author Comment

by:racineconde
ID: 34205929
Well I don't know how to get this value since there is an execution error when the runtime tries to run the query (beginning of the foreach loop).  
Is there a way to find out?
0
 
LVL 22

Expert Comment

by:p_davis
ID: 34205981
you can check all the values in the ctx.MyFiles. by putting a breakpoint and doing a quickwatch (if you are using visual studio)

also since it looks like this is a nullable date (assuming it is a date) you will want to check for null values (that could be the problem)

where (doc.Modified.HasValue && doc.Modified.Value.ToLongDateString().Equals(today))
0
 

Author Comment

by:racineconde
ID: 34206081
Yes those are data values and all the rows have values in that field.

I have changed the where clause to check the nullity of the value as you suggested but still get the same error.
0
 
LVL 22

Expert Comment

by:p_davis
ID: 34206126
how is the value getting set for the variable today?
0
 

Author Comment

by:racineconde
ID: 34206139
I have changed the code to the following:
******************************************
var docs = (from doc in ctx.MyFiles
                         select doc);
                foreach (var doc in docs)
                {
                    if (doc.Modified.Value.ToString("dd MMMM yyyy").Equals(today))
                    {
                        ModifiedFile modifedFile = new ModifiedFile();
                        modifedFile.FileName = doc.Name;
                        modifedFile.FileLocation = doc.Path;
                        modifedFile.DateLastModified = doc.Modified.Value;
                     }
                 }
******************************************
to remove the where clause from the query and this new code works. I consider this as a work-around and I am still interested (for my knowledge) to find out what's wrong with the where clause in the LINQ query.
0
 

Author Comment

by:racineconde
ID: 34206145
string today = DateTime.Now.ToString("dd MMMM yyyy");
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 22

Expert Comment

by:p_davis
ID: 34206159
what is ctx and MyFiles and how is today set.... i obviously am missing something and need the full picture of the setup.
0
 

Author Comment

by:racineconde
ID: 34206162
Initially I was doing the comparison directly and that's when I first got the error.
"where (doc.Modified.Value.AddDays(1).ToShortDateString() == DateTime.Now.ToShortDateString())"

That's when I decided to introduce the today variable thinking it might solve the problem.
0
 

Author Comment

by:racineconde
ID: 34206177
ctx is a datacontext variable referencing a sharepoint site and MyFiles is the name of the document library that contains the files I am trying to get hold of
0
 
LVL 22

Expert Comment

by:p_davis
ID: 34206307
have you tried just using the datetime? strings can act funky (and not the good james brown kind) when comparing dates.

try

where (doc.Modified.Value.AddDays(1).Date == DateTime.Now.Date)//use the .Date if the time portion of the date doesn't matter. otherwise just leave it off.

0
 

Author Comment

by:racineconde
ID: 34206931
after the modifs u suggested this is the error I get:
*******************************************
A first chance exception of type 'System.Data.Services.Client.DataServiceQueryException' occurred in System.Data.Services.Client.dll
************************************************
0
 
LVL 22

Expert Comment

by:p_davis
ID: 34207121
what id the regional settings for sharepoint site and your client? do they match?

are both set to format for en?
0
 
LVL 22

Expert Comment

by:p_davis
ID: 34207123
wow?? sorry

id = are
0
 
LVL 22

Expert Comment

by:p_davis
ID: 34207314
what if you try

where (doc.Modified.Value.AddDays(1).Date.ToString(CultureInfo.InvariantCulture.DateTimeFormat) == DateTime.Now.Date.ToString(CultureInfo.InvariantCulture.DateTimeFormat))
0
 
LVL 10

Expert Comment

by:Mathiyazhagan
ID: 34210583
I have tried to get last modified file of today with following code.used LastWriteTime property to get modified datetime . may it hleps you.
string sampleDirectory = Environment.GetFolderPath  (Environment.SpecialFolder.MyDocuments);

DirectoryInfo[] dirs = new DirectoryInfo (sampleDirectory).GetDirectories();

var docs = (from doc in dirs[0].GetFiles ()

	where (doc.LastWriteTime.ToLongDateString().Equals(DateTime.Today.ToLongDateString()))

	elect doc);

foreach (var doc in docs)

{

	ModifiedFile modifedFile = new ModifiedFile();

	modifedFile.FileName = doc.Name;

	modifedFile.FileLocation = doc.Directory.FullName ;

	modifedFile.DateLastModified = doc.LastWriteTime;

	Console.WriteLine(modifedFile);

}

 --- ModifiedFile class --

class ModifiedFile

{

   public string  FileName {get;set;}

   public string  FileLocation {get;set;}

   public DateTime  DateLastModified {get;set;}

					 

   public override string ToString()

   {

    return String.Format ("Name :{0} Path:{1} Date Modified                                        {2}",FileName,FileLocation,DateLastModified.ToLongDateString());

   }

}

Open in new window

0
 

Author Comment

by:racineconde
ID: 34211878
p davis: the modification did not fix the problem

Mathiyazhagan: I am getting the files from SharePoint. the "LastWriteTime" property does not exist.
0
 
LVL 12

Accepted Solution

by:
kumar754 earned 500 total points
ID: 35040482
var allModifiedFiles =
from doc in ctx.MyFiles.where (c => c.Modified.HasValue && c.Modified.Value.Date == DateTime.Today)
select new ModifiedFile()
{
  FileName = doc.Name,
  FileLocation = doc.Path,
  DateLastModified = doc.Modified.Value
};

this will return you a collection of ModifiedFile[] class and then you can play around with this collection or directly bind to a grid.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now