Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# How to return null or an model

I have Store Model that I want to return null if my model is null.

public Store Details() => db.Store.Single(s => s.Id == 1);

This query sometimes return a value and sometimes returns null. How can I specific the return type to include both?

like image 837
Millenial2020 Avatar asked Feb 25 '26 23:02

Millenial2020


1 Answers

Try to use

public Store Details() => db.Store.FirstOrDefault(s => s.Id == 1);
like image 57
karthik kasubha Avatar answered Feb 28 '26 12:02

karthik kasubha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!