← Tutti gli articoli
Filtering data in the Entity Data Framework
27 August 2012 ·
EDF · Article ·
385 visite
One of the most fundamental aspects of the Entity Framework querying is filtering data
- string contentTitle = "ASP.NET" ;
- bool orderByTitleAsc= true ;
- var query = from c in context.Contents select c;
- if (contentTitle != null )
- query = from c in query where c.ContentTitle == contentTitle select c;
- if (orderByTitle)
- query = from c in query orderby c.Title select c;;
- else
- query = from c in query orderby c.Title descending select c;
- foreach (Contents c in query)
- Console.WriteLine(c.Title );
LINQ allows any IQueryable to appear in the from part of another query, and this code takes advantage of this to augment the original query with additional operators.
Notice that the original query simply returns all contents.
If the contentTitle parameter is specified, the code adds a where clause to filter contents based on the specified value and replaces the original query.
You can also sort the query results based on the value of the orderByTitleAsc.
Notice that the original query simply returns all contents.
If the contentTitle parameter is specified, the code adds a where clause to filter contents based on the specified value and replaces the original query.
You can also sort the query results based on the value of the orderByTitleAsc.