Introduction
This article demonstrates an interesting and very useful concept in LINQ-to-SQL classes.
Question: What is select data with UDF via LINQ-to-SQL?
In simple terms "It enables selection of data with a User Defined Function (UDF) using LINQ-to-SQL communication".
Step 1: Create a new UDF
USE [Company]
GO
/****** Object: UserDefinedFunction [dbo].[GetEmployeeData] Script Date: 02/19/2013 21:59:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Function [dbo].[GetEmployeeData]
(@EmpId INT)
RETURNS TABLE
RETURNSelect [EmpId],[FirstName],[LastName],[Age]From [dbo].[tblEmployee]Where EmpId = @EmpId
Step 2: Create a new web application
Step 3: Adding LINQ-to-SQL classes
Step 4: Add a table and UDF onto the designer
Step 5: The complete code of WebForm1.aspx is as in the following:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SelectDataUDF_LINQtoSQL.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<center>
<div>
<table>
<tr>
<td colspan="2" align="center">
<asp:Label ID="Label1" runat="server" Text="Select Data with UDF via LINQ-to-SQL" Font-Bold="true" Font-Size="Large" Font-Names="Verdana"
ForeColor="Maroon"></asp:Label></td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Please Enter Id" Font-Bold="true" Font-Names="Verdana"></asp:Label></td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="Button1" runat="server" Text="Select Data" Font-Names="Verdana" Width="213px" BackColor="Orange" Font-Bold="True" OnClick="Button1_Click" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:GridView ID="GridView1" runat="server" CssClass="grid" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2"
ForeColor="Black" GridLines="None">
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<SortedAscendingCellStyle BackColor="#FAFAE7" />
<SortedAscendingHeaderStyle BackColor="#DAC09E" />
<SortedDescendingCellStyle BackColor="#E1DB9C" />
<SortedDescendingHeaderStyle BackColor="#C2A47B" />
<EmptyDataTemplate>No Records Found!</EmptyDataTemplate>
</asp:GridView>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Label ID="Label3" runat="server" Font-Bold="true" Font-Names="Verdana"></asp:Label></td>
</tr>
</table>
</div>
</center>
</form>
</body>
</html>
Step 6: The complete code of WebForm1.aspx.cs is as in the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SelectDataUDF_LINQtoSQL
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e){ }
protected void Button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(TextBox1.Text))
{
Label3.Text = "Please Enter Some Values";
Label3.ForeColor = System.Drawing.Color.Red;
}
else
{
var query = objContext.GetEmployeeData(int.Parse(TextBox1.Text));
GridView1.DataSource = query;
GridView1.DataBind();
Label3.Text = "Data Retrieved Successfully";
Label3.ForeColor = System.Drawing.Color.Green;
TextBox1.Text = string.Empty;
}
}
#region Instance MembersDataClasses1DataContext objContext = new DataClasses1DataContext();
#endregion
}
}
Step 7: The output of the application is as in the following:
Step 8: The selected data output of the application is as in the following:
I hope this article was useful for you.
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.

Gowtham RajamanickamPosted Apr 7, 2015, 6:24 AM
good one..