Link to home
Start Free TrialLog in
Avatar of kimmie8000
kimmie8000Flag for United States of America

asked on

Linq C# query where clause for date >= does not work

I am using a linq query for C# .NET.  

I have the following:

var date_now = DateTime.Now;

In the Linq Query I have

var query = from a in cs.table where a.change_date >= date_now orderby id select a;

Then I foreach each id...

It should only bring back 1 record.  However, it bring back the entire table..

I have googled and tried .value converting it to ToDateTime etc.  Nothing is working does anyone have an answer for me.  

Thanks..
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

How is change_date defined in the database?
Avatar of kimmie8000

ASKER

It is an sql datetime.
Can you provide a data sample?  I only ask because this:
using System;
using System.Linq;

namespace EE_Q28708185
{
	class Program
	{
		static void Main(string[] args)
		{
			DateTime now = DateTime.Now;
			EE_Q28708185_DatabaseEntities context = new EE_Q28708185_DatabaseEntities();
			var query = (from person in context.People where person.ChangedDate >= now select person);
			foreach (var person in query)
				Console.WriteLine("{0}; Changed: {1}", person.Name, person.ChangedDate.Value.ToShortDateString());

			Console.ReadLine();
		}
	}
}

Open in new window

Produces the following results -User generated imageFrom the following data model -User generated imageAnd the following data set -User generated image
-saige-
Hi kimmie8000;

I just tried some similar code on my system and it worked as expected and could not reproduce your issue.

Can you post the date and time value in date_now?
Can you post a couple of the date and time that are being returned by the query?
I tested with Linq to SQL. Is that what you are using or are you using Linq to Entity Framework?
If your current app in the small stage or large at this point?
    If small are you able to post it in a zip file.
    If you can do you have a test database that you can upload with the project?
If you can do the last question can you do that and post the link here?
    If you can not can you build a small test app with a test database  which exhibits the issue?
If It should only bring back 1 record, why do you order by and then loop? This makes no sense if you have only one record.

Have you verified the data, both in your variable and in the database?
The date and time variable is DateTime.Now;

The date I am trying to compare it to looks like this... '2015-08-04 21:39:14.000'

I have tried this logic with the compare that date >= DateTime.Now and it is cycling through each employee.  I just want bring back this employee.  The foreach is for other logic.  It can bring back more than 1 record, but I don't expect it to bring back the record for the date above for today 8/25/2015 and it does bring them back.
If, DateTime.Now;, has a value of lets say 2015-08-25 10:25:00.000 a day like this 2015-08-26 10:25:00.000 and the dates that follow it will be returned. Dates like 2015-08-24 10:25:00.000 and earlier will not be returned.
This is not what is happening in my current code.  It is returning all dates.
Seeming I can not reproduce your issue can you build a small test app with a db showing the issue so that we can attempt to find out what is happening?
It is like LINQ to SQL is ignoring my where clause.  This is a windows service.  

I change it to just look at one id.

var query = from a in cs.table
where (a.id = "some_id")
select a;

foreach(table a in cs.table)
{
}

If this simple query is not working, is it the foreach then?

I have had this work in the past, so I am very confused.
What you are describing cannot be reproduced by myself or Fernando.

As Fernando has requested, if you can provide a test project that emulates the behavior you are experiencing then we can see if we can provide some assistance.

-saige-
I think it might be my foreach loop.  It should have the query name in it.  I think.  In which case, I have just pounded my forehead into a wall.. Och!! I am testing this theory and I will get back to you on it.
That was it.  I am going to drink way more coffee!! GAWD... Sorry to have bugged you.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Helped me think through the logic.  The problem was not in the LINQ.  It was further down the code.  I feel like an idiot, but helped me talk through this code.