0

Trying to join 3 SQL tables with EF and LINQ, not sure how to write the right script. I know how to do it with 2 tables, but got a bit lost once a third table came into play.

I have 3 tables, the models look like that:

User:

public class User
{
    public int Id { get; set; }
    public DateTime Created { get; set; }
}

UserName:

public class UserName
{
    public int Id { get; set; }
    public int UserId { get; set; }
    public string Name { get; set; } = null!;
    public User User { get; set; } = null!;
}

UserNameValues:

public class UserNameValues 
{
    public int Id { get; set; }
    public int UserNameId { get; set; }
    public string Value { get; set; } = null!;
 
    public UserName UserName { get; set; } = null!;
}

I want to get the value from UserNameValues

where user.id = x and username.name = x

I appreciate any help!

1 Answer 1

1

Try this (assuming a variable called context with a collection of UserNameValues):

context.UserNameValues.Where(unv => unv.UserName.Name == "juan" && unv.UserName.User.Id == x);

I personally dislike the SQL-like syntax and stopped using it a long time ago in favor of the method calls, precisely because it's confusing.

Sign up to request clarification or add additional context in comments.

1 Comment

Last comment has no sense. Use query syntax when it is needed or you will start writing something ugly like this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.