Accepted answer

you could do it in one query by using:

private static long? getprice(string state, string vendor)
    return table
        .where(x => x.state == state)
        .orderbydescending(x => x.vendor)
        .firstordefault(x => x.state == state)

filter out all prices in the matching state, order by vendor descending (to make null values appear last), and pick the first or default item matching the state from it.


var value = this.context.table.firstordefault(a=>(a.vendor == "vendora" || a.vendor == null) && a.state == "la");


try to use find method:

var value = this.context.table.find(a=>a.vendor == "vendora" && a.state == "la");

please, see this performance considerations find() vs. where().firstordefault()


one call to database:

var values = this.context.table
    .where(a=>a.vendor == vendor && a.state == state || a.vendor == null 
        && a.state == state).tolist();

Related Query

More Query from same tag