I have already written several post about Linq its a great ORM that we can use in various way. The purpose of this post to demonstrate How we can bind custom entity to stored procedure result with use of Linq-To-SQL. Let’s go through it and you will realize that how easy it will be to bind a Custom Entity to Stored Procedure result.
Let’s first create a simple table to which will hold the user data. It will contain four field like UserId,UserName,FirstName and LastName like following.
Now let’s create a stored procedure which will return the table data and a new field called Full Name like following. Here full name is a combination of first name and last name
CREATE PROCEDURE dbo.GetAllUsers
SET NOCOUNT ON
FirstName + ' ' + LastName AS [FullName]
After adding class I have added same property as its having in user table and Hence our UserInfo Class will look like following.
<asp:GridView ID="grdUserList" runat="server">
protected void Page_Load(object sender, EventArgs e)
using (MyBlogDataContextDataContext myContext =
List<UserInfo> MyUserList =
grdUserList.DataSource = MyUserList;
That’s it its very easy.. Hope this will help you…