CRUD using Entity Framework in .NET Framework 5.0 - (Read)

About Author:

This article is provided by MVP Ming Man. Microsoft is so thankful that MVPs who proactively share their professional experience with other users. The article would be posted on MVP's website or blog later.
Summary
This article covers ways to read from Entity Framework; read all the records, filtering records, group records and sort records.



More information
Read all the records

The code below retrieves all the record from the product database table.

public static IEnumerable<Product> GetAllProduct()
{
//declare the data context from Entity Framework
 northwindEntities ctx = new northwindEntities();

//retrieve all record from the database table
var result = from r in ctx.Products select r;

return result;
}


Filtering records

The code below retrieves the record, by supplying the product id, from the product database table.

public IEnumerable<Product> GetProductById(int Id)
{
//declare the data context from Entity Framework
northwindEntities ctx = new northwindEntities();

//retrieve record from the database table by id
var result = from r in ctx.Products where r.ProductID == Id select r;

return result;
}


Group records

The code below retrieves all the record from the product database table and group by CategoryID.

public IQueryable<IGrouping<int?, Product>> GetProductGroupBy(int Id)
{
northwindEntities ctx = new northwindEntities();
var result = ctx.Products.GroupBy(prod => prod.CategoryID);
return result;

}


Sort records

The code below retrieves all the record from the product database table and sorted by CategoryID.

public IQueryable<Product> GetProductOrderBy(int Id)
{
northwindEntities ctx = new northwindEntities();
var result = ctx.Products.OrderBy(prod => prod.CategoryID);
return result;
}


 
Community Solutions Content Disclaimer

MICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.
Properties

Article ID: 2791121 - Last Review: 04/01/2013 02:23:00 - Revision: 2.0

Microsoft Visual Studio Agents 2012, Microsoft Visual Studio Express 2012 for Web, Microsoft Visual Studio Express 2012 for Windows 8, Microsoft Visual Studio Express 2012 for Windows Desktop, Microsoft Visual Studio Express 2012 for Windows Phone, Microsoft Visual Studio Premium 2012, Microsoft Visual Studio Professional 2012

  • kbmvp kbcommunity KB2791121
Feedback