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 Query
- Get first value from table with where either have the value match or null with LINQ
- How to get record form a different table based on a value from first table with linq expression?
- Get array of double from list of array of double, where first value in array is the max first value using linq
- Get all Elements where the first element grouped by 'X' property has 'Y' value not null
- When I'm trying to bring one value from a table with email, the result is always null
- C# LINQ - Get pairs, when second value is null in all pairs that have the same first value
- How to get the latest date inserted from the table with Linq to entities
- Joining two tables with LINQ while also returning null records from the second table
- Get from a List<t> the record with the maximum value of a specific property
- "Value cannot be null" while creating an XElement from the attributes where one or more value is null
- Get only one value from the table using LINQ
- How do I get the first value from this collection using Linq to Entities?
- If you have a LINQ statement using the Select() method, is there a way to get a value from the next record?
- Get the objects with the maximum value for a specific property from a list using LINQ
- Linq - Get all keys from dictionary where all values in the value list are equal to criteria
- How to get the property from a list of object that is a string with the lowest numeric value
- Get data from a table after joining based on null value of joined table using LINQ
- Comparing a list of CSV values in Linq to match those from second list with any value in first list
- get selected value from combobox with data source is anonymous type
- 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
- Get a cell value from datatable with two condition in where clause with LINQ
- How to get the next match from a MatchCollection following after a specific Capture.Index with Linq
- Finding results using the EF4 in a table where they don't exist or don't match a value
- Does Linq in Entity Framework code first use SQL or does it get the whole table first?
- 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 write SQL and LINQ to get the one record from a result with where condition?
- How to get first row from DB using Entity Framework where column starts with certain string and continues with numeric characters
- How to get a column value from data table with Linq
- getting values from a table where value is not null
More Query from same tag
- How to add Where Conditions in LINQ Joins
- Can someone explain Query Reshaping in Linq with the following example?
- SqlQuery returning ora-00911 invalid character although the query generated runs well in Oracle database
- Json.net Deserialization Where properties may not exist
- Add if conditional into anonymouse type
- The Property method can only be used with primitive or complex properties. Use the Reference or Collection method
- Linq Group By & Sum Query On Multiple Columns
- How to use contains (T-SQL) in Entity Framework
- Linq to Sql including 2 children sets
- I am wondering about the state of connection and impact on code performance by 'yield' while iterating over data reader object
- Return Timespan ticks difference in LINQ queries
- Using a native SQL Server function in Nhibernate?
- Retrieve all item in listbox and select only those which is in db
- Negating a method call in an Expression tree
- The specified type member is not supported in LINQ to Entities
- EntityDataSource and DropDown
- How to generate SqlFunctions.DateDiff dynamically
- LINQ to SQL advice - why won't it work?
- How to commit changes immediately after a linq to sql truncate
- Advanced LINQ Update Statement
- "Where" clause after "ProjectTo" and Nullable types?
- LINQ expression instead of nested foreach loop
- how to convert MM/dd/YYYY hh:mm:ss AM to YYYY-MM-dd datetime format by linq?
- Cartesian products with n number of list
- LINQ to SQL lambda not clear
- Find items in one list based on items in another list of a different type
- INNER JOIN LEFT JOIN in LINQ to SQL
- Export an IQueryable Collection to Excel
- Entity Framework LINQ many to many query
- How can I create a list of rows that have one of a list of properties