February 3, 2011

Select Sub Fields 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 sub fields from table below is the LINQ Query

Default1 .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 Default1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataClassesDataContext db = new DataClassesDataContext();
        var results = from o in db.Orders


                      select new
                      {
                          o.iOrderNo,
                          o.sSenderName 
                          
                      };




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


    }
}


Default1 .aspx

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


<!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>

No comments:

Post a Comment