Linq to Entities multiple where clause RRS feed

  • Question

  • Hi,

    I have a simple EF model with Person and PersonName objects. They are joined by a one to many association from Person to PersonName. The PersonName object contains a StartDate and EndDate. I am trying to write a linq to entities query to filter out by Person.Id and PersonName.StartDate/PersonName.EndDate. I am struggling to do this!

    var query = from per in _context.People
       where per.Id == id
       select query;

    The above works when filtering Person.Id, but I cannot work out how to filter on StartDate and EndDate as well.

    Many Thanks...

    Monday, August 1, 2011 10:24 AM

All replies

  • Hi,

    Check with this,

        public class Person
          public int Id { get; set; }
          public IList<PersonName> PersonNames = new List<PersonName>();
        public class PersonName
          public DateTime StartDate { get; set; }
          public DateTime EndDate { get; set; }
        List<Person> personList = new List<Person>();
        int id = 0;
        var persons = from p in personList
               where p.Id == id && p.PersonNames.Any<PersonName>(pN => { return pN.StartDate >= DateTime.Now.AddDays(-10); })
               select p;


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    • Proposed as answer by Kris444 Friday, August 5, 2011 2:29 PM
    Monday, August 1, 2011 2:33 PM