DEV Community

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

alivaterocket profile image
Alivate • Edited on

You shouldn't use the join keyword, a second "from" is best:

i) you get to use the equality operator;
ii) you can arrange the operands any way you like;
iii) you can add on the DefaultIfEmpty() function on the end to change to a left join and vice versa.

private User GetUserByToken(string token)
    var user = (from u in _context.Users
        from t in _context.Tokens.Where(T => t.UserId == u.Id)
        where t.Body == token
        select u).SingleOrDefault();

    return user;

Also, I always use the LINQ format instead of chained functions. Simply because it's more readable.

sduduzog profile image
Beautus S Gumede Author

This is so true. The join was unnecessary 🤔 I was kind of suprised to why the Lambda version I switched to seems shorter, it was because it didn't use a join 😬