0

My classes

public class User
{
    [Key]
    public long Id {get;set;}
    public ICollection<AuthToken> Tokens { get; set; } = new List<AuthToken>();
}

public class AuthToken
{
    [Key]
    public long id { get; set; }
    public string Token { get; set; }
    public long Created { get; set; }
    public long Dispose { get; set; }
    public string Ip { get; set; }
    public string LocalisationInfo { get; set; }
    public string MachineInfo { get; set; }
}

I want to select AuthToken object, which string Token is equal to ``` 123 `

var user = cnx._Users
              .Where(x => x.Tokens.Contains(/*Tokens.Token object equal to 123 */))

In simply Where condition I can only compare two AuthToken objects. I need to compare AuthToken.Token strings.

Finally I want to select User who has Token I used. In this case its 123.

2 Answers 2

4

You can 'chain' multiple linq calls to get the result you want...

var user = cnx._Users.Where(u => u.Tokens.Any(atoken => atoken.Token == "123")).FirstOrDefault();
Sign up to request clarification or add additional context in comments.

Comments

1

You can also achieve it like below

var user = cnx._Users.Where(x => x.Tokens.FirstOrDefault(t => t.Token == "123") != null);

Comments

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.