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();
Source: stackoverflow.com
Related Articles
- Get first value from table with where either have the value match or null with LINQ
- Comparing a list of CSV values in Linq to match those from second list with any value in first list
- How to get record form a different table based on a value from first table with linq expression?
- getting values from a table where value is not null
- When I'm trying to bring one value from a table with email, the result is always null
- Joining two tables with LINQ while also returning null records from the second table
- Linq with where clause in many-to-many EF Code First object
- How to query by where clause with EF code first
- "Value cannot be null" while creating an XElement from the attributes where one or more value is null
- LINQ update column in table 1 with id from table 2 where emails match?
- Can't add a new record with an integer value into database by using linq from code C#
- Select Value from List Column Where Name Match
- Linq query for distinct data from a table with where condition
- Best way to add null value to IEnumerable as First Record with an example
- Get data from a table after joining based on null value of joined table using LINQ
- get selected value from combobox with data source is anonymous type
- Using value from related table in LINQ where clause
- How to take the Max value from a List of object where the same objects exists with many duplicate rows
- Left join two tables with a default value when no match is found in the right table
- Linq query to Join tables if value not in first table fetch from second
- Get a cell value from datatable with two condition in where clause with LINQ
- Linq select a value based on where condition from other table
- OrderBy with value from another table
- Fetch value from database table where one column of table(foreign key) is equal to input value
- Finding results using the EF4 in a table where they don't exist or don't match a value
- Remove from a list where a elements value is null
- Linq: Select from 2 datatable where column id from first table = column id from second table
- Fetching value from a datatable into datatable with where clause
- EF. Select records from one table based on the properties of the records from second table that have an FK from the first
- How to get first row from DB using Entity Framework where column starts with certain string and continues with numeric characters
- Get a new list of a custom type using a Group by
- How to Select Min and Max date values in Linq Query
- The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties
- Flattening a loop with lookups into a single linq expression
- C# linq to entity include with condition and ordering
- List Intersect returning null
- How does IEnumerable<T> work in background
- Delete Columns from datatable
- Zipcode to city/state look-up XML file?
- making Linq case sensitive
- LINQ how get max rating
- Grouping by anonymous v.s. grouping by non-anonymous
- linq: grandparent - parent - children query
- Deduplicate XML using LINQ based on attribute value
- querying a collection against a byte property in mongodb with c# driver
- LINQ statement optimization
- Querying n:m-Relationships with LINQ
- How to group more than two tables using Linq
- Querying an ICollection in ViewModel
- Add nested values from nested class lists in Linq