score:2

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)
        ?.Price;
}

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.

score:0

var value = this.context.table.FirstOrDefault(a=>(a.vendor == "VendorA" || a.vendor == null) && a.state == "LA");

score:0

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()

UPDATE:

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 Articles