How to Select Data from different Tables Using Linq

Answered How to Select Data from different Tables Using Linq

  • martedì 1 maggio 2012 16:31
     
      Contiene codice

    Hi,
    I have two different tables, I just want to collect data from tables using Linq to SQL Queries.
    The tables looks like This 
     
    ID Name ImageUrl 
    Other Table is
    ID EmpID CheckInTime CheckOutTime 
    What I want to Collect data from CheckInTime and want to place it in a that is in a list view
    Same thing I want to do it for CheckOutTime And One thing I want to tell is both tables are joined by a FK EmpID with ID.
     
    What Are the suggestions for me 

    I have Used this code

     var data = from emp in db.Employees
    
                               join chk in db.CheckInCheckOuts on emp.ID equals chk.EmpID
                               select new EmployeeCheckInOut
                               {
                                   Name = emp.Name,
                                   ImageUrl = emp.ImageUrl,
                                   CheckIn = emp.CheckInCheckOuts,
                                   CheckOut = emp.CheckInCheckOuts
                               };

    Here the CheckInCheckOuts is another table, I don't how do I access fields of the Other table "CheckInCheckOuts"
    Thank you
    Ali

Tutte le risposte

  • martedì 1 maggio 2012 16:44
     
     
    What kind of tables are your talking about? It looks more generic lists to me then some DataTables.

    Mitja

  • martedì 1 maggio 2012 16:56
     
     

    Mitja,

    Kind of Tables, I don't Know but I can Tell you that these are Two table, first Table Have Data in It, Name, ImageUrl I have filled this table with names and ImageUrls And are string type.Other Table is for the CheckInTime And CheckOutTime of the employee.

    What I need that when I click on the Image button it Should displays The Current Datetime into the label below the Image button.

    So I have Problem accessing my CheckInCheckOut Table because I may not have Idea about.Did you understand what I need to do, if you have more question please ask to me.

    Thanks

    Ali


    • Modificato qqaadir martedì 1 maggio 2012 16:57
    •  
  • martedì 1 maggio 2012 17:36
     
     Con risposta Contiene codice

    if tables are connected with foreign key, then you can retrieve data using following way

    var data = from emp in db.Employees
    Select emp.Name, emp.ImageUrl,emp.CheckInCheckOuts.Intime,
    emp.CheckInCheckOuts.Outtime;

    No need to join in query if they have  joins in entites.


    Hasibul Haque,MCC,MCPD hasibulhaque.com

  • martedì 1 maggio 2012 19:06
     
     

    Yes this is useful 

    thank you