Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
How to Select Data from different Tables Using Linq

Answered How to Select Data from different Tables Using Linq

  • 2012년 5월 1일 화요일 오후 4:31
     
      코드 있음

    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

모든 응답

  • 2012년 5월 1일 화요일 오후 4:44
     
     
    What kind of tables are your talking about? It looks more generic lists to me then some DataTables.

    Mitja

  • 2012년 5월 1일 화요일 오후 4: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


    • 편집됨 qqaadir 2012년 5월 1일 화요일 오후 4:57
    •  
  • 2012년 5월 1일 화요일 오후 5:36
     
     답변됨 코드 있음

    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

  • 2012년 5월 1일 화요일 오후 7:06
     
     

    Yes this is useful 

    thank you