MohdRash
MohdRash

Reputation: 123

Joining two tables with one to many relatipnship in entity framework code first

i have

  public class Menu
    {  
       public int ID { get; set;} 
       public List<Task> Tasks { get; set; }
    }



 public class Task
    {
        public int ID { get; set; }
        public byte[] Image { get; set; }
        public string Name { get; set; }  
    }

i would like to know all tasks which has a certain List ID using LINQ queries

Upvotes: 1

Views: 154

Answers (2)

user1793607
user1793607

Reputation: 531

Try

var result = Menus.Where(menu => menu.ID == id)
                  .Select(menu => menu.Tasks)
                  .FirstOrDefault();

Also you may want to peruse http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b as this would answer most of your queries like the above.

Upvotes: 1

huMpty duMpty
huMpty duMpty

Reputation: 14460

You can use Enumerable.Where

var list = Tasks.Where(l=>l.ID ==x);

or

   var list = from t in Tasks
              where t.ID == x
              select t;

x will be the id you need to compare

Upvotes: 0

Related Questions