none
How to Get data RRS feed

  • Question

  • For example I have 3 class 


          

    public class Enterprise
        {
            public virtual int Id { get; set; }   

            public virtual ICollection<Work> Works { get; set; }

        }

           

      public class Work
        {
            public virtual int WorkId { get; set; }

            public virtual ICollection<Task> Tasks { get; set; }

    }

     public class Task
        {

            public virtual int TaskId { get; set; }

    }

    How I can Get all Task belong some enterprize??????

    var query1 = db.Tasks.Where(d => enterprize.Works.Contains(d.Work)).ToList(); NOT WORK

    Monday, August 21, 2017 10:18 AM

All replies

  • Hi Ahlompys,

    If you use Linq to SQL, you could use DataLoadOptions to achieve it, the following sample code for your reference.

    using (var db = new DataClasses1DataContext())
                {
                    var dlo = new DataLoadOptions();
                    dlo.LoadWith<Enterprise>(p => p.Works);
                    dlo.LoadWith<Work>(w => w.Tasks);
                    db.LoadOptions = dlo;
    
                    var result = db.Enterprises.ToList();
    
                    foreach (var item in result)
                    {
                        Console.WriteLine("Id = {0}, name = {1}", item.Id, item.Name);
    
                        foreach (var sitem in item.Works)
                        {
                            Console.WriteLine("WorkId = {0}, entid = {1}", sitem.WorkId, sitem.EntId);
    
                            foreach (var subitem in sitem.Tasks)
                            {
                                Console.WriteLine("TaskId = {0}, workId = {1}", subitem.TaskId, subitem.WorkId);
                            }
                        }
                    }
                    Console.ReadKey();

    If you use LINQ to entity, we could use include method to achieve it, please check the following document(see Eagerly loading multiple levels).

    https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx

    Best regards,

    Cole


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 22, 2017 2:02 AM
    Moderator