Chanikya
Chanikya

Reputation: 486

How to get Values of another table using one table using LINQ

I've Three tables Master,Users,Category like below

  Master Table :                                Users Table :     Category Table :
+---+------------+-----------+------------+     +----+------+    +----+-------+
|ID | CategoryID | CreatedBy | UpdatedBy  |     | ID | Name |    | ID | Name  |
+---+------------+-----------+------------+     +----+------+    +----+-------+
| 1 |    1       |    1      |     1      |     | 1  | Ravi |    |  1 | Fruit |
| 2 |    2       |    2      |     1      |     | 2  | Balu |    |  2 | Toys  |
| 3 |    2       |    2      |     2      |     +----+------+    +----+-------+
+---+------------+-----------+------------+ 

In Master table CretedBy and UpdatedBy id's are referenced to Users table. I wrote the LINQ to get the data from Users and Category tables like below :

var result = (from p in db.Master join q in db.Category on p.CategoryID equals q.ID join r in db.Users on p.CreatedBy equals r.ID select new {p,q,r}).ToList();

From Here i can able to get the CreatedBy value from Users table. But, also how to get the value of UpdatedBy from Users table using LINQ?

Can you please help me?

Thanks in Advance !!!!!

Upvotes: 0

Views: 409

Answers (1)

CodeNepal
CodeNepal

Reputation: 902

You need to join one more time just like you would write an SQL Query

    var result = (
    from p in db.Master 
    join q in db.Category on p.CategoryID equals q.ID 
    join u1 in db.Users on p.CreatedBy equals u1.ID 
    join u2 in db.Users on p.UpdatedBy equals u2.ID 
    select new {
        p, 
        q, 
        u1,
        u2,
    }).ToList();

Upvotes: 1

Related Questions