views:

51

answers:

4

Why is this query returns 0 lines?

There is a record matching the arguments.

SomeDataContext db = new SomeDataContext(ConnString);

return db.Deafkaw.Where(p => 
      (p.SomeDate1 >= aDate && 
         p.SomeDate1 <= DateTime.Now) &&
      (p.Year == aYear && p.IsSomething == false)
  ).ToList();

Am i missing something?

On the Table Deafkaw

SomeDate1 = 20/4/2010 11:32:17 Year = 2010 IsSomething = False

...besides other columns im not interested in conditions.

I need SomeDate1 between the dates i give IsSomething = False and Year = 2010.

A: 

Try

Deafkaw.Where(p => (p.ImerominiaKataxorisis >= aDate && p.ImerominiaKataxorisis <= DateTime.Now &&
            p.Year == etos && p.IsYpodeigma == false)).ToList();
Christopher Edwards
David
Yeap i think its the same, s=not working anyway i tried.
Christopher Edwards
+2  A: 

It is difficult to answer your question without any additional information. Checking the following points may help you to find the problem:

  • If you remove Where clause and write Deafkaw.ToList(), what do you get?
  • What is the value of aDate and etos?
  • Can you double check the condition? Do you require that all subconditions hold at the same time? Are there any such data if you print entire DeaFkaw data structure?
  • Can you try removing some sub-conditions to see if that gives you some results?
Tomas Petricek
If i use Deafkaw.ToList() i get the data from the table properly, i need all these conditions to hold at the same time as you mean al are true...in a meaning, removing subconditions, still dont return what i need.strange isn't it
So, are you saying that if you write for example `Deafkaw.Where(p => p.ImerominiaKataxorisis <= DateTime.Now).ToList()`, you still get 0 results? Is that true for all sub-conditions (e.g. `IsYpodeigma == false`?), What happens when you write `Deafkaw.Where(p => true).ToList()`? Does that return some results?
Tomas Petricek
I don't know why but i had to introduce a variable and return this variable to work!!! Other similar methods working direct in return statement.
That sounds weird. Could you edit your question and show the difference? Perhaps somebody could explain the behavior :-)
Tomas Petricek
+3  A: 

You aren't assigning the result to anything so it is being discarded. Try this:

var results = db.Deafkaw.Where(p => 
         (p.ImerominiaKataxorisis >= aDate && 
          p.ImerominiaKataxorisis <= DateTime.Now) &&
         (p.Year == etos && p.IsYpodeigma == false)
     ).ToList();

Update: you changed the question so now I'm not sure that this is the correct answer. Can you post the code where you call this method?

Mark Byers
Oh, I just assumed that he just wasn't including an assignment in his message.
Chris Farmer
Basically .ToList() returns in a method.
Adding var results and returning results worked!!!
@gtas: I am as surprised as you are. But it's good you have solved your problem.
Mark Byers
A: 

Use SQL profiler. Look at the sql query that is generated. Run the sql query manually and see if you get back any records.

Raj Kaimal
You could also just set the `Log` property of your data context: `db.Log = Console.Out;` will output the query and bind parameters to the console, for example.
Chris Farmer