none
Why is this Include not working? RRS feed

  • Question

  •  

    Hi,

      I have three related tables: Invoices, Items and Products.

      My Linq to Entities query is like this:

      Invoices.Include("Items").Include("Items.Products");

      It works. The SQL query is correct.

      Now I only wish the Invoice number and the name of the Product. So the query turns this:

      Invoices.Include("Items").Include("Items.Products").Select(s => new { s.Number, s.Items.Products.Name });

      The s.Items.Products is not working. I don't have Products in Items all I have are Sum, Min, etc. What is wrong? The relation of Items to Invoices is MANY, the relation of Products to Items is 1.

      Thanks,

      Marco Castro

     


    Thursday, December 29, 2011 12:29 AM

Answers

  • Oh I find the problem,

      Correct me if I'm wrong.

      To do what I wish I can't use Items as Include because Include creates a list to each Invoice line. My query must be like this:

      from inv in Invoices
      from items in inv.Items
      select new { inv.Number, items.Products.Name };

      How can this be wriiten using dots as I did before? How can I place Include is this situation?

      Thanks,

      Marco Castro

    Thursday, December 29, 2011 1:52 AM

All replies

  • Hi Marco;

    Can you please post the schema of the three tables.

    Thanks


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, December 29, 2011 1:14 AM
  • Hi Fernando,

      The schema are:

      Invoices is related to Items as

      Associatoin: FK_ITENSNOT_REFERENCE_NOTASSAI
      From Role: Invoices
      Multiplicity: * (Many)
      To Role: Items

      Items are related to Products as

      Associtation: FK_ITENSNOT_LIVROS_DO_LIVROS
      From Role: Items
      Multiplicity: 1 (One)
      To Role: Products

      In SQL the foreigh keys are:

      FK_ITENSNOT_REFERENCE_NOTASSAI

      ALTER TABLE ITEMS
      ADD CONSTRAINT FK_ITENSNOT_REFERENCE_NOTASSAI
        FOREIGN KEY (INVOICE) REFERENCES INVOICES(INVOICE) ON DELETE CASCADE; 

      FK_ITENSNOT_LIVROS_DO_LIVROS

      ALTER TABLE ITEMS
      ADD CONSTRAINT FK_ITENSNOT_LIVROS_DO_LIVROS
      FOREIGN KEY (PRODUCT) REFERENCES PRODUCTS(PRODUCT) ON DELETE CASCADE ON UPDATE CASCADE;

      Is it what you wish?

      Thanks,

      Marco Castro


    Thursday, December 29, 2011 1:32 AM
  • Oh I find the problem,

      Correct me if I'm wrong.

      To do what I wish I can't use Items as Include because Include creates a list to each Invoice line. My query must be like this:

      from inv in Invoices
      from items in inv.Items
      select new { inv.Number, items.Products.Name };

      How can this be wriiten using dots as I did before? How can I place Include is this situation?

      Thanks,

      Marco Castro

    Thursday, December 29, 2011 1:52 AM