Need help Joining 5 Tables in LinQ

Posted on 2014-07-10
Last Modified: 2016-02-16

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 ?
Question by:Vikash p
    LVL 29

    Expert Comment

    by:Gautham Janardhan
    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
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    What do you mean by "whole raw?"

    Author Comment

    by:Vikash p
    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.
    LVL 74

    Accepted Solution

    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).

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
    Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
    Via a live example, show how to shrink a transaction log file down to a reasonable size.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now