mardi 30 janvier 2018

Lambda expression inside ?? (if shortcut) operator

I like short code, so recently I was trying to fit if statement and Linq query with lambda expression in one line.

Is it possible to do something like:

db.Users.First(x => x.UserId == providedId)??x.SomeUsefullField

That should return null if user not found, and value of some field if user does exists.

Is it possible to do without first getting user and than getting field value like this:

var user = db.Users.First(x => x.UserId == providedId);
var fieldValue = user.SomeUsefullField;

Thank you for possible solution and knowledge sharing.

Aucun commentaire:

Enregistrer un commentaire