Search code examples
c#asp.netasp.net-mvcpococ#-7.0

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?


Solution

  • Try to use

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