none
Linq - I can't get Include method in Linq RRS feed

  • Question

  • hi  .......

     

    i am work with vs 2008 with linq . i create a dbml with two related tables Category and Subcategory .

    i try to  Category.include("Subcategory") to fetch related table data. i can't get the include method in

    my vs intellisense . please help


    i am sorry not vs 2008 its Microsoft Visual C# 2010
    • Moved by Leo Liu - MSFT Monday, June 20, 2011 5:30 AM Moved for better support. (From:Visual C# Language)
    • Edited by RageshShiva Monday, June 20, 2011 9:20 AM error specifying version
    Friday, June 17, 2011 4:43 AM

Answers

  • Make sure you have:

    using System.Linq;

    ...in the top of your file.  Visual Studio has the nasty habit of cutting that line from your code if you happen to use the "remove unused using statements" command from the Organize usings flyout.  This has gotten me a few times myself.

    Evan

     

    Friday, June 17, 2011 6:21 PM
  • Hi RageshShiva;

    The code sample below is using the Northwind sample database to show how to Lazy Load and Eager Load data from a related table. Please read the comments in the code

    DataClasses1DataContext ctx = new DataClasses1DataContext();
    
    // This code uses Lazy Loading which means that when you query a table that
    // has a relationship with another table those values will be loaded as needed
    // in the code. So in this example I query the Orders table and when I iterate 
    // through the values I can get the values of the Order_Details for the order.
    // Each time you access another Orders Order_Detail a new query is sent to the
    // Server to get the data. This option is good when you do not need the Order_Details
    // for each Order
    
    // Get the orders table
    List<Order> orders = (from ord in ctx.Orders
               select ord).ToList();
    
    // iterate through the Orders
    foreach (var order in orders)
    {
      // If the Order has shipped print the Order_Details of the order
      if(order.ShippedDate != null)
      {
        // Iterate through the Orders Order_Details
        foreach (var orderdetail in order.Order_Details)
        {
          // Print some information from the table
          Console.WriteLine("Product ID : {0} Quantity : {1} Discount : {2}",
                   orderdetail.ProductID, orderdetail.Quantity, orderdetail.Discount);
        }
      }
    }
    

    using System.Data.Linq;
    
    
    DataClasses1DataContext ctx = new DataClasses1DataContext( );
    
    // Set the Data Load Option of the DataContect to load Order_Details
    // for each Order returned from the query
    DataLoadOptions options = new DataLoadOptions( );
    options.LoadWith<Order>( detail => detail.Order_Details );
    ctx.LoadOptions = options;
    
    // Because of the Data Load Option above all Orders will have there 
    // collection of Order_Details already loaded, This is called Eager Loading.
    
    // Get the orders table
    List<Order> orders = ( from ord in ctx.Orders
                select ord ).ToList( );
    
    // iterate through the Orders
    foreach( var order in orders )
    {
      // Iterate through the Orders Order_Details
      foreach( var orderdetail in order.Order_Details )
      {
        // Print some information from the table
        Console.WriteLine( "Product ID : {0} Quantity : {1} Discount : {2}",
                 orderdetail.ProductID, orderdetail.Quantity, orderdetail.Discount );
      }
    }
    
    


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, June 18, 2011 4:09 AM

All replies

  • Hello Ragesh

    Here is one link. I hope it will help you

    http://stackoverflow.com/questions/5064129/linq-query-to-count-by-distinct-category-subcategory

    Thanks


    Sankarshan Parida.Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Friday, June 17, 2011 6:35 AM
  • On 6/17/2011 12:43 AM, RageshShiva wrote:
    > hi .......
    >
    > i am work with vs 2008 with linq . i create a dbml with two related
    > tables Category and Subcategory .
    >
    > i try to Category.include("Subcategory") to fetch related table data. i
    > can't get the include method in
    >
    > my vs intellisense . please help
    >
     
    Did you delete the Linq reference in the project and put it back?
     
    Friday, June 17, 2011 4:59 PM
  • Make sure you have:

    using System.Linq;

    ...in the top of your file.  Visual Studio has the nasty habit of cutting that line from your code if you happen to use the "remove unused using statements" command from the Organize usings flyout.  This has gotten me a few times myself.

    Evan

     

    Friday, June 17, 2011 6:21 PM
  • Hi RageshShiva;

    The code sample below is using the Northwind sample database to show how to Lazy Load and Eager Load data from a related table. Please read the comments in the code

    DataClasses1DataContext ctx = new DataClasses1DataContext();
    
    // This code uses Lazy Loading which means that when you query a table that
    // has a relationship with another table those values will be loaded as needed
    // in the code. So in this example I query the Orders table and when I iterate 
    // through the values I can get the values of the Order_Details for the order.
    // Each time you access another Orders Order_Detail a new query is sent to the
    // Server to get the data. This option is good when you do not need the Order_Details
    // for each Order
    
    // Get the orders table
    List<Order> orders = (from ord in ctx.Orders
               select ord).ToList();
    
    // iterate through the Orders
    foreach (var order in orders)
    {
      // If the Order has shipped print the Order_Details of the order
      if(order.ShippedDate != null)
      {
        // Iterate through the Orders Order_Details
        foreach (var orderdetail in order.Order_Details)
        {
          // Print some information from the table
          Console.WriteLine("Product ID : {0} Quantity : {1} Discount : {2}",
                   orderdetail.ProductID, orderdetail.Quantity, orderdetail.Discount);
        }
      }
    }
    

    using System.Data.Linq;
    
    
    DataClasses1DataContext ctx = new DataClasses1DataContext( );
    
    // Set the Data Load Option of the DataContect to load Order_Details
    // for each Order returned from the query
    DataLoadOptions options = new DataLoadOptions( );
    options.LoadWith<Order>( detail => detail.Order_Details );
    ctx.LoadOptions = options;
    
    // Because of the Data Load Option above all Orders will have there 
    // collection of Order_Details already loaded, This is called Eager Loading.
    
    // Get the orders table
    List<Order> orders = ( from ord in ctx.Orders
                select ord ).ToList( );
    
    // iterate through the Orders
    foreach( var order in orders )
    {
      // Iterate through the Orders Order_Details
      foreach( var orderdetail in order.Order_Details )
      {
        // Print some information from the table
        Console.WriteLine( "Product ID : {0} Quantity : {1} Discount : {2}",
                 orderdetail.ProductID, orderdetail.Quantity, orderdetail.Discount );
      }
    }
    
    


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, June 18, 2011 4:09 AM
  • Hello Ragesh,

    How's your issue? Do the any suggestions work for you? If so, please mark the useful reply as answer. IF you have any updates, please feel free to let us know.

     

    have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 20, 2011 7:07 AM
    Moderator