LinqConnect Documentation
In This Topic
    Extracting Data
    In This Topic
    Extracting Data
    LinqConnect Documentation
    Extracting Data
    [email protected]

    In this article we use a model created with the help of Entity Developer (read more about it in the section Using Entity Developer) or created manualy (read more about it in the section Creating LinqConnect Classes Manually).

    Each database table is represented as a Table collection, accessible via the GetTable() method using its entity class to identify it. It is recommended that you declare a strongly typed DataContext instead of relying on the basic DataContext class and the GetTable() method. A strongly typed DataContext declares all Table collections as members of the context.

    CautionNote:

    To run samples from this article include the following namespace before your code:

    using CrmDemoContext;
    
    
    Imports CrmDemoContext
    
    

    Use the following examples for data extracting:

    // DataContext takes a connection string
    CrmDemoDataContext db = new CrmDemoDataContext();
    
    // Get a typed table to run queries
    Table<Company> Companies = db.GetTable<Company>();
    
    // Query for companies from Boston
    var BostonCompanies =
         from c in Companies
         where c.City == "Boston"
         select c;
    foreach (var comp in BostonCompanies)
       Console.WriteLine("id = {0}, City = {1}", comp.CompanyID, comp.City);
    ' DataContext takes a connection string
    Dim db As New CrmDemoDataContext
    
    ' Get a typed table to run queries
    Dim Companies As Table(Of Company) = db.GetTable(Of Company)()
    
    ' Query for companies from Boston
    Dim BostonCompanies = 
         From c in Companies _
         Where c.City = "Boston" _
         Select c
    For Each comp in BostonCompanies
       Console.WriteLine("id = {0}, City = {1}", comp.CompanyID, comp.City)
    Next
    

    The query for companies from Boston can be expressed in a simpler way:

    CrmDemoDataContext db = new CrmDemoDataContext();
    var BostonCompanies =
         from c in db.Companies
         where c.City == "Boston"
         select c;
    foreach (var comp in q)
       Console.WriteLine("id = {0}, City = {1}", comp.CompanyID, comp.City);
    Dim db As New CrmDemoDataContext
    Dim BostonCompanies = 
         From c In db.Companies _
         Where c.City = "Boston" _
         Select c
    For Each comp in BostonCompanies
       Console.WriteLine("id = {0}, City = {1}", comp.CompanyID, comp.City)
    
    Next
    

    You can read more about LINQ queries in the Querying Across Relationships topic