简体   繁体   中英

join multiple tables using LINQ

I want to join these 3 tables on Primary key of SPID.

picture Table
[PIC_ID]   INT             
[pic_name] VARCHAR     
[SPID]     INT        
[pic]      VARBINARY (MAX)

service_provider table
[SPID]           INT           
[Sp_email]       VARCHAR 
[Sp_password]    VARCHAR 
[Sp_name]        VARCHAR   
[Sp_location]    VARCHAR 
[City_ID]        INT

city Table
[City_ID]  INT         
[Cityname] VARCHAR    

To join above tables on SPID I have Written below Query.

var sp_details = (from s in db.service_provider
                              join p in db.pictures on s.SPID equals p.SPID 
                              join c in db.cities on s.City_ID equals c.City_ID
                              where s.SPID == id
                              select new ImageData()
                              {
                                  SPID = s.SPID,
                                  Sp_name = s.Sp_name,
                                  Sp_location = s.Sp_location,
                                  Cityname = c.Cityname,
                                  service_type = s.service_type,
                                  Sp_description = s.Sp_description,
                                  Sp_rate = s.Sp_rate,
                                  Sp_web = s.Sp_web,
                                  Cnt_wh = s.Cnt_wh,
                                  pic = p.pic
                              });

In my case picture table doesn't contain values for a instance. then How do I avoid join picture table with other tables using same code and return sp_details to VIEW without pic. I appreciate If someone can help me to solve this problem.

If I understood you correctly, you want to get the element even if they don't have an image, so you should use right join it should return all the rows from the right part of the join even id they don't have an image.

var sp_details = (from s in db.service_provider
                              right join p in db.pictures on s.SPID equals p.SPID 
                              join c in db.cities on s.City_ID equals c.City_ID
                              where s.SPID == id
                              select new ImageData()
                              {
                                  SPID = s.SPID,
                                  Sp_name = s.Sp_name,
                                  Sp_location = s.Sp_location,
                                  Cityname = c.Cityname,
                                  service_type = s.service_type,
                                  Sp_description = s.Sp_description,
                                  Sp_rate = s.Sp_rate,
                                  Sp_web = s.Sp_web,
                                  Cnt_wh = s.Cnt_wh,
                                  pic = p.pic
                              });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM