none
Parent without Child records RRS feed

  • Question

  • I have a "Product" object that has a Many to Many relationship with an "Element" object.  The "Element" object has a Many to One relationship with an "Architecture" object  and a Many to One relationship to a "Component" object.  I would like to pass in a "Product" ID and return all the "Elements" that do not have "Architecture" children with relationship to the "Product".  I also need the "Component" fields.  Could someone show me how to do this?

    Thanks.

     public class Product
        {
               
            public int ID { get; set; }
            public string ProductCode { get; set; }
            public string ProductDescription { get; set; }
            public string Family { get; set; }
            public string HPDCode { get; set; }
            public string LastModUser { get; set; }
            public DateTime LastModDate { get; set; }
           
            //Navigation
            public virtual ICollection<Component> Components { get; set; }
            public virtual ICollection<Element> Elements { get; set; }
        }

     public class Element
        {

            public int ID { get; set; }
            public double Weight { get; set; }
            //public double Quantity { get; set; }
            public string LastModUser { get; set; }
            public DateTime LastModDate { get; set; }

            public int ComponentID { get; set; }
            public int SupplierID { get; set; }
              
            //Navigation
            public virtual Component Component { get; set; }
            public virtual Supplier Supplier { get; set; }
            public virtual ICollection<Attachment> Attachments { get; set; }
            public virtual ICollection<Architecture> Architectures { get; set; }
            public virtual ICollection<Product> Products { get; set; }
        }

     public class Component
        {
            public int ID { get; set; }
            public string ComponentCode { get; set; }
            public string ComponentDescription { get; set; }
            public double ComponentWeight { get; set; }
            public string ComponentComment { get; set; }
            public string LastModUser { get; set; }
            public DateTime LastModDate { get; set; }

            //Navigation
            public virtual ICollection<Attachment> Attachments { get; set; }
            public virtual ICollection<Product> Products { get; set; }
            public virtual ICollection<Element> Elements { get; set; }
        }

      public Architecture()
            {
                this.MinPercent = 0;
                this.MaxPercent = 0;
            }

            public int ID { get; set; }
            public double MinPercent { get; set; }
            public double MaxPercent { get; set; }
            public string LastModUser { get; set; }
            public DateTime LastModDate { get; set; }

            //FK's  
            public int ElementID { get; set; }
            public int ChemicalID { get; set; }

            //Navigation
            public virtual Chemical Chemical { get; set; }
            public virtual Element Element { get; set; }
    }

    Monday, April 20, 2015 8:46 PM

All replies

  • Hi New to Linq,

    You could use the eager loading in Entity Framework.

    You could include the needed relationship like below.

    using (var context = new BloggingContext()) 
    { 
        // Load all blogs and related posts 
        var blogs1 = context.Blogs 
                              .Include(b => b.Posts) 
                              .ToList(); 
     
        // Load one blogs and its related posts 
        var blog1 = context.Blogs 
                            .Where(b => b.Name == "ADO.NET Blog") 
                            .Include(b => b.Posts) 
                            .FirstOrDefault(); 
     
        // Load all blogs and related posts  
        // using a string to specify the relationship 
        var blogs2 = context.Blogs 
                              .Include("Posts") 
                              .ToList(); 
     
        // Load one blog and its related posts  
        // using a string to specify the relationship 
        var blog2 = context.Blogs 
                            .Where(b => b.Name == "ADO.NET Blog") 
                            .Include("Posts") 
                            .FirstOrDefault(); 
    }

    #Loading Related Entities
    https://msdn.microsoft.com/en-us/data/jj574232.aspx

    If you have any other concern regarding this issue, please feel free to let me know.

    Best regards,
    Youjun Tang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 22, 2015 6:05 AM