none
How to use "Include" conditionaly in linq query? RRS feed

  • Question

  • I have a linq query below. I want "Include" GroupDetail1 or GroupDetail2 conditionally based on the value of GroupID. Please see the question below in the code itself

    public class Main
        {
            public void DoWork(int personID)
            {
                // get the package hierarchy from the database
                IRepository<Person> repository = DefaultServiceLocator.Instance.GetInstance<IRepository<Person>>();
                var package = repository.GetQuery()
                    .Where(p=>p.PersonID == personID)
                    .Include(p=>p.PersonAddress) 
                    .Include(p=>p.PersonGroup)                
                    //???? here i would like to include 
                    //???? if p.PersonGroup.GroupID = 1 then include GroupDetails1 
                    //???? if p.PersonGroup.GroupID = 2 then include GroupDetails2                
            }
        }
    
        public class Person
        {
            public int PersonID { get; set; }
            public Address PersonAddress { get; set; } 
            public Group PersonGroup { get; set; }
            public IList<GroupDetail1> GroupDetails1 { get; set; }
            public IList<GroupDetail2> GroupDetails2 { get; set; }
        }
    
        public class Address
        {
        }  
    
        public class Group
        {
            public int GroupID { get; set; }
        }
    
        public class GroupDetail1
        {    
        }
    
        public class GroupDetail2
        {
        }
    

     


    • Edited by lax4u Thursday, January 12, 2012 8:24 PM
    Thursday, January 12, 2012 8:23 PM

Answers

  • Hi lax4u,

    Welcome to MSDN Forum.

    Please try to the code below

    public class Main
        {
            public void DoWork(int personID)
            {
                // get the package hierarchy from the database
                IRepository<Person> repository = DefaultServiceLocator.Instance.GetInstance<IRepository<Person>>();
    
                var package = repository.GetQuery()
                    .Include(p=>p.PersonAddress)
                    .Include(p=>p.PersonGroup)
                    .Where(p=>p.PersonID == personID)
                    .First();
    
                if(package.PersonGroup.GroupID==1)
                    {
                          var finalPackage = repository.GetQuery()
                                                        .Include(p=>p.PersonAddress)
                                                        .Include(p=>p.PersonGroup)
                                                        .Include(p=>p.GroupDetails1)
                                                        .Where(p=>p.PersonID == personID)
                     }
    
                else if(package.PersonGroup.GroupID==2)
                    {
                          var finalPackage = repository.GetQuery()
                                                        .Include(p=>p.PersonAddress)
                                                        .Include(p=>p.PersonGroup)
                                                        .Include(p=>p.GroupDetails2)
                                                        .Where(p=>p.PersonID == personID)
    
                     }                           
            }
        }
    
    

    Best Regards
    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 16, 2012 4:53 AM
    Moderator

All replies

  • Hi lax4u,

    Welcome to MSDN Forum.

    Please try to the code below

    public class Main
        {
            public void DoWork(int personID)
            {
                // get the package hierarchy from the database
                IRepository<Person> repository = DefaultServiceLocator.Instance.GetInstance<IRepository<Person>>();
    
                var package = repository.GetQuery()
                    .Include(p=>p.PersonAddress)
                    .Include(p=>p.PersonGroup)
                    .Where(p=>p.PersonID == personID)
                    .First();
    
                if(package.PersonGroup.GroupID==1)
                    {
                          var finalPackage = repository.GetQuery()
                                                        .Include(p=>p.PersonAddress)
                                                        .Include(p=>p.PersonGroup)
                                                        .Include(p=>p.GroupDetails1)
                                                        .Where(p=>p.PersonID == personID)
                     }
    
                else if(package.PersonGroup.GroupID==2)
                    {
                          var finalPackage = repository.GetQuery()
                                                        .Include(p=>p.PersonAddress)
                                                        .Include(p=>p.PersonGroup)
                                                        .Include(p=>p.GroupDetails2)
                                                        .Where(p=>p.PersonID == personID)
    
                     }                           
            }
        }
    
    

    Best Regards
    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 16, 2012 4:53 AM
    Moderator
  • wouldnt that hit the database twice? i trying to do it one database call
    Tuesday, January 17, 2012 5:26 PM
  • Hi lax4u,

    Because the GroupID is the condition of 'Include' method, so you have to get the 'GroupID' first. If you write them in one query statement, when it hit the database and query data, it can't tell the second step what the GroupID's value.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Thursday, January 19, 2012 2:21 AM
    Moderator