1

I'm trying to do a left join that is 1 user record has 1 (if it's there) contact Record associated with it. When this runs, it still pull multiple contacts ignoring my condition of 'where c.RecordType == "USR"'.

 public class Users
{
    public int Id { get; set; }
    ... other properties

     public Contact Contact { get; set; }
}

    public class Contact
{
    public int Id { get; set; }
    public int RecordID { get; set; }
    public string RecordType { get; set; }

    [ForeignKey("RecordID")]
    public Users User { get; set; } 
}

Linq Query

var query3 = (from c in db1.Contacts
                      join u in db1.Users
                      on c.RecordID equals u.Id
                      into a
                      from b in a.DefaultIfEmpty(new Models.Users())
                      where c.RecordType == "USR"
                      && b.Lastname.Contains(name)
                      && b.Active == 1
                      select new
                      {
                          b.Id,
                          b.FirstName,
                          b.Lastname,
                          b.FullName,
                          b.Contact                          
                      }).ToList();

Any help with where I'm going wrong would be much appreciated.

Thanks, Adam

3
  • without seeing your data it is hard to tell but Are you certain that your foreign key is 'RecordID' ? without seeing your data I am guessing that is your problem?
    – DaniDev
    Oct 11, 2018 at 22:59
  • RecordID is the foreign key. What is throwing me off it is still pulling records that have a 'RecordType' other than 'USR'. I'm trying to tinker around with it in LinqPad, but no luck getting it to filter correctly with my original query or the ones below.
    – brachen33
    Oct 12, 2018 at 14:32
  • Can you show us a some sample data? did you try simplifying your query as @Jim Rhodes suggested. His Linq query should get you what you wan if your tables and keys are designed correctly
    – DaniDev
    Oct 12, 2018 at 20:27

2 Answers 2

1

You can do a join in linq without using the join keyword. I use the following construct for left joins and do not have any issues.

var query3 = (from c in db1.Contacts
              from u in db1.Users.Where(x => c.RecordID == x.Id).DefaultIfEmpty() 
              where (c.RecordType == "USR")
                  && u.Lastname.Contains(name) && (u.Active == 1)
              select new
              {
                  u.Id,
                  u.FirstName,
                  u.Lastname,
                  u.FullName,
                  u.Contact                          
               }).ToList();
0

As per information in question

1 user record has 1 (if it's there) contact Record associated with it

that means, a user can have zero or more contacts, so you suppose to use User as main entity and then left join on Contacts. Also you should apply Contact type filter during JOIN itself, like below

var query3 = (  join u in db1.Users
              from c in db1.Contacts 
                on new { Record = c.RecordID, Type = c.RecordType } equals new { Record = u.Id, Type = "USR"} into b
              from cont in b.DefaultIfEmpty(new Models.Contacts())
              where u.Lastname.Contains(name)
              && u.Active == 1
              select new
              {
                  u.Id,
                  u.FirstName,
                  u.Lastname,
                  u.FullName,
                  cont.Contact                          
              }).ToList();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.