0

I am trying to filter linked table data using lambda .Include() with the following:

var jobs = db.jobs.Include(d => d.docs)
           .Where(d => d.docs.startdate >= date1 && d => d.docs.enddate <= date1);
return View(jobs);

This doesn't work though. Does anyone know what the correct syntax is to filter the linked table data correctly? Thanks!

UPDATE:

d.docs.startdate
d.docs.enddate

".startdate" and ".enddate" are not showing up in intellisense when I use d.docs.startdate or d.docs.enddate. I have linked the table relationships and saved the changes but even after using the .Include() command in code, intellisense cannot find the startdate and enddate fields in the docs table through intellisense.

ERROR: 'System.Collections.Generic.ICollection' does not contain a definition for 'duedate' and no extension method 'duedate' accepting a first argument of type 'System.Collections.Generic.ICollection' could be found (are you missing a using directive or an assembly reference?)

...Not sure what I'm missing here?

2 Answers 2

2

Include() after the Where() statement:

var jobs = db.jobs.Where(d => d.docs.startdate >= date1 && d.docs.enddate <= date1)
             .Include(d => d.docs);
return View(jobs);
6
  • Also remove the second d=> should be d => d.docs.startdate >=date 1 && d.docs.endate
    – PW Kad
    Commented Jun 7, 2013 at 21:17
  • This keeps failing because ".startdate" and ".enddate" are not showing up in intellisense however the table IS linked properly in the database relationships. How come this field is not popping up in intellisense? Commented Jun 11, 2013 at 17:35
  • Try closing the file and visual studio. Then reopening everything.
    – Gabe
    Commented Jun 11, 2013 at 18:15
  • Didn't work. I'd like to post my question but I'm not sure if I should do it here. Should I make a new post and link to that one? Commented Jun 11, 2013 at 18:18
  • Yea, I would post a new question because it's a different problem. This answer solves the original problem.
    – Gabe
    Commented Jun 11, 2013 at 18:19
-1

You should not have two lambdas within the Where(). So your query should look like this:

var jobs = db.jobs.Where(d => d.docs.startdate >= date1)
                  .Where(d => d.docs.enddate <= date1)
                  .Include(d => d.docs);
return View(jobs);

or both the Where's in one lambda (what you like more, both has the same result):

var jobs = db.jobs.Where(d => d.docs.startdate >= date1 && d.docs.enddate <= date1)
                  .Include(d => d.docs);
return View(jobs);

The Where() and the Include() does not mean the same. Where() filters your data, while Include() just eager load your data.

5
  • Removing the second d => is correct but you can always use two clauses in the where statement
    – PW Kad
    Commented Jun 7, 2013 at 21:16
  • Yes you can, but I think it is more readable this way. It doesn't make a different which way you use. Is my answer wrong or why do you downvote it? Commented Jun 7, 2013 at 21:18
  • 2
    Because it is not true, you are saying the issue is in the pattern, not answering the question
    – PW Kad
    Commented Jun 7, 2013 at 21:19
  • With saying "not have two lambdas" I meant not having the "d =>" twice. Think that does answering the question. I edited my answer and also posted the second way with having both conditions in one lambda. Commented Jun 7, 2013 at 21:22
  • This keeps failing because ".startdate" and ".enddate" is not showing up in intellisense however the table IS linked properly in the database relationships. How come this field is not popping up in intellisense? Commented Jun 11, 2013 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.