DEV Community

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

Collapse
 
alienroid profile image
Alienroid • Edited

Great! Now try lambda expression, you will be happier:)

Collapse
 
sduduzog profile image
Sdu

I did 😅 it looks way better if you're working on a single table

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
Sdu

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
Sdu

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;