none
Join in LINQ RRS feed

  • Question

  • Hi I have the bellow syntax.
                    var results = 
                        ctx.pln_trainers.Where(p => p.is_active == true)
                        .Include(p => p.pln_planificare)
                        .ToList();
    What I want to do is to make a join for p.pln_planificare with a table pln_users. In pln_planificare I have a column named username and in pln_users I have a column named username. Suplimentary in pln_users I have the name of the person so I want to make a join in order to bring the name of the person instead of username.

    Thanks in advance, Ciprian LUPU

    • Moved by Kevin Linq Thursday, September 22, 2016 9:41 AM
    Wednesday, September 21, 2016 4:11 PM

Answers

  • Hi Ciprian Lupu,

    According to your description and related code, please try the following code, which use inner join.

    var query = (from p in db.pln_trainers
                                 join pp in db.pln_planificares on p.Id equals pp.TrainerId
                                 join u in db.pln_users on pp.username equals u.username
                                 where p.is_active == true
                                 select new
                                 {
                                     Id = p.Id,
                                     IsActive = p.is_active,
                                     Name = u.Name
                                 }).ToList();

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, September 23, 2016 1:56 AM
    Moderator

All replies

  • https://msdn.microsoft.com/en-us/library/bb397941.aspx

    https://www.linqpad.net/

    Do it in Linqpad first and then you can just copy the code...

    There are also plenty of examples in Linqpad too.

    Wednesday, September 21, 2016 5:35 PM
  • try something like this:

    
    var JoinResult = (from p in pln_users.AsEnumerable()   
                     join t in pln_planifcare.AsEnumerable()   
                     on p.Field<string>("username") equals t.Field<string>("username")   
                     select new   
                     {   
                         is_active = p.Field<string>("is_active"),   
                         username = p.Field<string>("username"),   
                         personName = t.Field<string>("personName"),     
                     }).ToList(); 


    Rich P

    Wednesday, September 21, 2016 5:54 PM
  • Hi  Ciprian Lupu :

    Thank you for posting here.

    Since your issue is related to Linq to SQL , I will move your thread to Linq to SQL forum . 

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Here is the link of LINQ forum:https://social.msdn.microsoft.com/Forums/en-US/home?forum=linqtosql

    If you have anything else regarding C# programming , please feel free to let me know . 

    Best Regards,

    Kevin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Thursday, September 22, 2016 9:40 AM
  • Hi Ciprian Lupu,

    According to your description and related code, please try the following code, which use inner join.

    var query = (from p in db.pln_trainers
                                 join pp in db.pln_planificares on p.Id equals pp.TrainerId
                                 join u in db.pln_users on pp.username equals u.username
                                 where p.is_active == true
                                 select new
                                 {
                                     Id = p.Id,
                                     IsActive = p.is_active,
                                     Name = u.Name
                                 }).ToList();

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, September 23, 2016 1:56 AM
    Moderator
  • if I will use Include than I will have a list in a field, if I will use a join instead of Include things are going in a different manner.

    Thanks in advance, Ciprian LUPU

    Friday, September 23, 2016 7:02 AM
  • Hi Ciprian Lupu,

    As far as I know, If you want to retrieve the filed of name via username, it seems that you need to use join method to retrieve related name.

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 26, 2016 8:23 AM
    Moderator