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..
kimmie8000Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
How is change_date defined in the database?
kimmie8000Author Commented:
It is an sql datetime.
it_saigeDeveloperCommented:
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 -Capture.JPGFrom the following data model -Capture.JPGAnd the following data set -Capture.JPG
-saige-
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Fernando SotoRetiredCommented:
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?
Jacques Bourgeois (James Burger)PresidentCommented:
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?
kimmie8000Author Commented:
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.
Fernando SotoRetiredCommented:
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.
kimmie8000Author Commented:
This is not what is happening in my current code.  It is returning all dates.
Fernando SotoRetiredCommented:
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?
kimmie8000Author Commented:
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.
it_saigeDeveloperCommented:
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-
kimmie8000Author Commented:
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.
kimmie8000Author Commented:
That was it.  I am going to drink way more coffee!! GAWD... Sorry to have bugged you.
it_saigeDeveloperCommented:
It's no bother.  Glad you got it sorted out.

-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kimmie8000Author Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.