February 3, 2011

Filter data using Linq to SQL

How to use Linq To Sql  refer my below post
http://www.dotnetissues.com/2011/02/select-query-linq-to-sql-example.html

To Select filtered fields from table below is the LINQ Query

Default2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>


<!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 runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server"/>
    </div>
    </form>
</body>
</html>


Default2.aspx.cs


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataClassesDataContext db = new DataClassesDataContext();
        var results = from o in db.Orders
        where (o.iDelieveryTypeId ==10)
                      select new
                      {
                          o.iOrderNo,
                          o.sSenderName


                      };




        GridView1.DataSource = results;
        GridView1.DataBind();


    }
}

No comments:

Post a Comment