score:3
Use the Linq Contains() method
http://msdn.microsoft.com/en-us/library/system.linq.enumerable.contains.aspx
var booked = db.Exhibitors.Select(s => s.StandID).ToList();
var stand = db.Stands.Where(s=>s.Booked == false
&& !booked.Contains(s.StandId)
.Select(s => new SelectListItem
{
Value = s.StandID.ToString(),
Text = s.StandNumber + ": " + s.Description + ": " + s.Size + ": £" + s.Rate.ToString()
});
I see you updated your question about best practices, too. There's a lot of optimization that can be done to your technique. First, read about the differences between IEnumerable
and IQueryable
.
Specifically speaking, you should remove the ToList()
from Stands.ToList().Where()
. By performing ToList()
before Where()
you're bringing back all of your rows from the db, and then processing them in memory. This could be very costly. If you perform Stands.Where(...)
then your filtering will be done in sql and only the rows you want will be returned.
Generally, save your ToList() to be your final step, and actively decide if you need the list to be constructed at all.
score:0
Ideal way is always to create a model that you want as output, map the output to that model and return it. This will also help the client side to get a definitive model to stick to and implement.
Also complex select queries in LINQ are generated on the fly which takes more time than a well structured stored procedure, which is pre-compiled. Consider this if you are expecting faster responses.
Source: stackoverflow.com
Related Articles
- asp.net mvc Linq exclude values from other table
- linq query to join two tables and get the count from one table values from the other
- Linq to select data from one table not in other table
- creating Linq to sqlite dbml from DbLinq source code
- LINQ select from table where fieldValue is in another tables list of values
- Exclude null values while summation by specific column in a left joined table using LINQ
- C# - LINQ - Sum a field from other table
- How to return values from a LINQ query and display them in a table using C#, ASP.NET MVC and Entity Framework
- VB.Net Linq with datatables - select from one table what does not exist in the other
- LINQ statement getting value from one table depending on the other table
- C# Linq with datatables - select from one table what does not exist in the other
- LINQ retrieve values from a table that of which fields(of a certain column) are not equal of another table
- Linq group by multiple columns and getting extra values from table before group by clause
- Linq query selects values from first row only in table
- Select values from one table based on specific value of another table Linq
- LINQ - I need to pull records from table 2 based on values in table 1
- LINQ to SQL to find values starting with characters from another table
- LINQ query for grouping a table based on the values from another table
- Get columns values in data table using linq from Entity Framework
- Linq select a value based on where condition from other table
- Linq assign values from one table into another
- LINQ query to conditionally filter on a where clause and select the values from another table
- LINQ: join multiple Table Column using Linq and find aggregated sum from child table values
- How can I return a single value integer from Linq from an array of objects that have other nested values
- How to reinsert data from one table onto itself using LINQ in code migration?
- selecting a column values from a Table using LINQ (Entity Framework)
- LINQ sql expression to exclude data that is present in other table
- Retrieving all items from one table that is not in other table with Linq
- LINQ Query - exclude the values of one list from another list
- Is there a possibility to take 100 records then other 100 record from database table using LinQ and Take()
- Build LINQ query in a loop
- Building a Complex Search Filter based on Linq
- C# Groupby Linq and foreach
- How do I combine two lists to a List<KeyValuePair<>>?
- Grouping by month with LINQ
- linq & distinct, implementing the equals & gethashcode
- How to calculate the sum of all values in a dictionary excluding the first item's value?
- Is there a more succinct Linq expression for inserting a constant between every item in a list?
- IEqualityComparer and singleton
- Finding items that are contained in two lists as substrings
- Linq 'into' keyword confusion
- LINQ find first occurrence before and after target value of equal distance
- Remove same value in Lists within a Dictionary in C# - LINQ
- RavenDB query with Linq and enum
- Creating Entity Frame work via model first in visual studio
- Determine if collection is of type IEnumerable<T>
- Pattern Match on all words not just whole
- Add/change the DataServiceQuery<T> in Silverlight
- How to combine result of multiple Linq Expressions into one Expression?
- Remove records from 1 query based on same values found in another query