DEV Community

loading...

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

Collapse
fernandomondo profile image
Fernando Mondo

You can use navigation properties and lambda to improve your code.

Something like:

public class User
{
public virtual Token Token { get; set; }
}

And then:

_context.Users.SingleOfDefault(u => u.Token.Body == Token);

Thread Thread
sduduzog profile image
Beautus S Gumede Author

Ok, this actually looks cleaner and more concise. I'm assuming from this that the virtual property relation to users is setup automatically because both entities exist in one context. I'm puzzled by how this really works

Thread Thread
sduduzog profile image
Beautus S Gumede Author

Here's another strange thing I'm picking up from this snippet, the fact that the property name and its type can just be exactly the same without C# complaining. I've been experimenting areas where such is possible. public virtual Token Token;