• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 483
  • Last Modified:

Need help Joining 5 Tables in LinQ

Hi,

I have 5 tables .
Table A with Person ID as Primary Key
Table B with Service ID as Primary key
Table C Service ID as FK
Table D Service ID as FK

Table E Service ID as FK

Now when user search by Service ID I need to join 5 tables to send all data . Is there any option to pass whole raw from particular table without mentioning all fields ?
0
Vikash p
Asked:
Vikash p
  • 2
1 Solution
 
Gautham JanardhanCommented:
do u mean something like this
var d = from emp in Employee 
             join dep in Department on dep,Id = emp.DepartmentId
             select dep;

Open in new window



here select dep would return all column in department
0
 
käµfm³d 👽Commented:
What do you mean by "whole raw?"
0
 
Vikash pAuthor Commented:
Gautam You have joined just two tables I need code how to do this on multiple table.

Kaufmed whole raw means::  i need to return data from 5 tables . 2 tables have 20+ columns and i need all columns value . so don't want to specify each column like TableC.Column1, TableD.Column2 for those tables rather want to return like ( TableC.allcolumns, tableD.AllColums, TableA.Column1, Table2. Column2) from other 3 tables i want to return only 4/5 columns from each table.
0
 
käµfm³d 👽Commented:
You can expand gauthampj's example to include the remaining tables as:

var query = from a in TableA 
            join b in TableB on a.PersonID equals b.PersonID
            join c in TableC on b.ServiceID equals c.ServiceID
            join d in TableC on b.ServiceID equals d.ServiceID
            join e in TableC on b.ServiceID equals e.ServiceID
            select new
            {
                TableA = a,
                TableB = b,
                TableC = c,
                TableD = d,
                TableE = e
            };
            
foreach (var row in query)
{
    var v1 = row.TableA.PersonID;
    var v2 = row.TableB.ServiceID;
}

Open in new window


But note that there's an extra layer to go through. In the Select part of the query, you are basically selecting the whole row and assigning it to the property name (lines 8 - 12). That means when you actually interrogate the data (lines 15 - 19) you have to include this extra layer (lines 17 & 18).
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now