none
How to include hierarchy in LINQ queries? RRS feed

  • Question

  •  public class Main
        {
            public void Run(int id)
            {
                using (IRepository<A> repository = DefaultServiceLocator.Instance.GetInstance<IRepository<A>>())
                {
                    var package = repository.GetQuery()
                       .Where(a => a.ID == id)
                       .Include(a => a.ClassBCollection.Select(b => b.ClassC))
                       .SingleOrDefault();
    
                    // the above query works but i also wanted to include 
                    // ClassD (in ClassB)                
                    
                    // How do i re-write the query to include the heirarchy i wanted 
                }
            }
        }
    
        public class A
        {
            public int ID { get; set; }
    
            public IList<B> ClassBCollection { get; set; }
        }
    
        public class B
        {
            public C ClassC { get; set; }
    
            public D ClassD { get; set; }
        }
    
        public class C
        {
            
        }
    
        public class D
        {
    
        }
    
       
    


    • Edited by lax4u Monday, February 6, 2012 10:23 PM
    Monday, February 6, 2012 8:39 PM

Answers

All replies

  • Hi lax4u,

    Welcome to MSDN Forum.

    Please try to modify the query statement as below

    var package = repository.GetQuery() .Where(a=>a.ID==id) .Include(a=>a.ClassBCollection) .Include(a=>a.ClassBCollection.Select(b=>b.C)) .Include(a=>a.ClassBCollection.Select(b=>b.D)) .SingleOrDefault();

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us


    Wednesday, February 8, 2012 9:09 AM
    Moderator
  • thanks that worked

    but let say if modif the class B as below. Now the class B has IsExcluded() boolen method, how do i select only not execluded class B coellection

        public class B
        {
            public C ClassC { get; set; }
    
            public D ClassD { get; set; }
            
            public bool IsExcluded()
            {
            }
        }

     public class Main
        {
            public void Run(int id)
            {
                using (IRepository<A> repository = DefaultServiceLocator.Instance.GetInstance<IRepository<A>>())
                {
                   var package = repository.GetQuery()
                       .Where(a=>a.ID==id)
                       .Include(a=>a.ClassBCollection.Where(x=>x.IsExcluded()==false) // this doesnt work
                       .Include(a=>a.ClassBCollection.Select(b=>b.C))
                       .Include(a=>a.ClassBCollection.Select(b=>b.D))
                       .SingleOrDefault();
                }
            }
        }

    Wednesday, February 15, 2012 7:39 PM
  • Hi lax4u,

    Try to modify the 'Where' to 'Any'.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, February 28, 2012 7:01 AM
    Moderator