locked
C# Entity LINQ Include another table into collection with .Include() RRS feed

  • Question

  • User956626884 posted

    I am trying to data from another table into my query with .include() but not sure how.

    In my class I got the following I got a second .include and I would in include the data from the Title table where the titleId equals the Id from Table1 table. Thanks.

    Classes:

    public class Table1
    {
        public int Id { get; set; }
        public string Name { get; set; }
    
        [System.Runtime.Serialization.IgnoreDataMember]
        public virtual System.Collections.Generic.ICollection<Address> Address { get; set; }
    
        [System.Runtime.Serialization.IgnoreDataMember]
        public virtual System.Collections.Generic.ICollection<Titles> Titles { get; set; }
    
        public Table1()
        {
    
            Address= new System.Collections.Generic.List<Address>();
            Titles= new System.Collections.Generic.List<Titles>();
        }     
    }
    
    public class Titles 
    {
        public int titleId { get; set; }
        public string Name { get; set; }     
    }
    

    Query:

     var query = (from e in ctx.Table1
                 .Include(t1 => t1.Address.Select(p => Address))
                 .Include(rj => rj.Titles.Where(t2 => t2.titleId == t1.id) // I would like to do this
                 select e).ToFullyLoaded();
    Tuesday, August 6, 2019 4:23 PM

All replies

  • User1520731567 posted

    Hi comicrage,

    Query:

     var query = (from e in ctx.Table1
                 .Include(t1 => t1.Address.Select(p => Address))
                 .Include(rj => rj.Titles.Where(t2 => t2.titleId == t1.id) // I would like to do this
                 select e).ToFullyLoaded();

    According to your Titles class, titleId is a normal property, not a navigation property.

    However Include only accepts a navigation property.

    And what is ToFullyLoaded() mean?

    Is it your custom method?

    I suggest you could modify,like:

       var query = ctx.Table1
    .Include(t1 => t1.Address.Select(p => p.ObjectiveDetail))
    .Include(rj=> rj.Titles).Where(t2 => t2.titleId == yourvariable)
    .ToList();

    If you have any questios,please post more details,such as: Address class...

    Best Regards.

    Yuki Tao

    Wednesday, August 7, 2019 5:58 AM
  • User956626884 posted

    Hi Yuki,

    Thanks for your help but when I was rewriting my code as your specified, I could not access the Titles Id field.

    var query = ctx.Table1.Include(rj => rj.Titles).Where(t2 => t2. <=== I did not see titleId, only the Titles table. 

    I am using .net 4.7 and EF 6.3.0

    I did I miss something?

    Thanks

    Wednesday, August 7, 2019 3:12 PM
  • User1520731567 posted

    Hi comicrage,

    Could you post your complete entity and your screenshot of Intelligent selection or error message?

    Best Regards.

    Yuki Tao

    Thursday, August 8, 2019 9:26 AM