DEV Community

Discussion on: C# and .NET Core Appreciation Post. The most beautiful piece of code I have ever seen... this month!

Collapse
 
juancri profile image
JC Olivares • Edited

I like LINQ syntax, but I think it's not really necessary here. I'd write it as:

private User GetUserByToken(string tokenBody)
{
    var token = _context.Tokens.SingleOrDefault(t => t.Body == tokenBody);
    if (token == null)
        return null;

    var user = _context.Users.SingleOrDefault(u => u.Id == token.UserId);
    return user;
}

Of course, this is a subjective matter, so any approach is OK if the team is fine with it.