entity framework - retrieving a single entity
LINQ to entities has a pair of methods, First and FirstOrDefault. These methods return the first item in the result set. When we know that only one item is present in the result set, we can use either Single or SingleOrDefault methods.
The below code demonstrates how to construct a query using the Single method.
In the above code, we have first defined the query and then called the Single() method. So, the query is executed first and then customer entity is returned.
Note that if the result set does not contain any items, the Single and First methods will throw an exception. The FirstOrDefault and SingleOrDefault will not throw an exception but return default value usually null in c# and Nothing in vb.
If the result set contains more than one value and if you are using SingleOrDefault or Single, an exception will be thrown. Here, we should use First or FirstOrDefault.
The below code demonstrates how to construct a query using the Single method.
using (var context = new SalesOrderManagementEntities()) { IQueryableCustomers = from cust in context.Customer where cust.CustomerId==1 select cust; Customer customer = Customers.Single(); Console.WriteLine(customer.Name); Console.ReadLine(); }
In the above code, we have first defined the query and then called the Single() method. So, the query is executed first and then customer entity is returned.
Note that if the result set does not contain any items, the Single and First methods will throw an exception. The FirstOrDefault and SingleOrDefault will not throw an exception but return default value usually null in c# and Nothing in vb.
If the result set contains more than one value and if you are using SingleOrDefault or Single, an exception will be thrown. Here, we should use First or FirstOrDefault.
Entity Framework Articles
- entity framework entity data model
- entity framework orm object relational mapper
- querying entity data model using objectcontext
- using ado net entity data model in windows applications.html
- create entity data model using ado.net entity framework designer in visual studio
- create-sales-order-management-data-model-using-edmgen-tool.html
- display data in a gridview using ado.net entity data source control
- ways of querying entity data model
- query entity data model using linq to entities
- query entity data model with object services and entity sql
- query entity data model using linq methods
- parameterized objectquery against a entity data model
- query entity data model entity client
- linq to entities query with projections in c#
- entity framework - save changes to entities
- entity framework - insert new objects
- add an entity to associated entities
- deleting entities using deleteobject() method of objectcontext
- entity framework load method
- eager loading using include method in entity framework
Most Viewed
Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls
VB .Net
Constructors in Visual Basic.Net