2008/10/19

LINQ 語法簡介

介紹幾個基本的LINQ語法

LinqSample.aspx:


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

<!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>LINQ Sample</title>
<style type="text/css">
.style1
{
font-size: xx-large;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<span class="style1">Linq Sample</span><br />
<br />
<asp:Button ID="ButtonFromAndSelect" runat="server" Text="select"
onclick="ButtonFromAndSelect_Click" />
<asp:Button ID="ButtonWhere" runat="server" Text="where"
onclick="ButtonWhere_Click" />
<asp:Button ID="ButtonForeach" runat="server" Text="foreach"
onclick="ButtonForeach_Click" />
<asp:Button ID="ButtonSelectNew" runat="server" Text="select new"
onclick="ButtonSelectNew_Click" />
<asp:Button ID="ButtonOrderBy" runat="server" Text="orderby"
onclick="ButtonOrderBy_Click" />
<asp:Button ID="ButtonTake" runat="server" Text="take"
onclick="ButtonTake_Click" />
<asp:Button ID="ButtonSkipAndTake" runat="server" Text="skip and take"
onclick="ButtonSkipAndTake_Click" />
<asp:Button ID="ButtonInsert" runat="server" Text="insert"
onclick="ButtonInsert_Click" />
<asp:Button ID="ButtonUpdate" runat="server" Text="update"
onclick="ButtonUpdate_Click" />
<asp:Button ID="ButtonDelete" runat="server" Text="delete"
onclick="ButtonDelete_Click" />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>


LinqSample.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 LinqSample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
// LINQ : Select
protected void ButtonFromAndSelect_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();
var dt = from pt in northwind.Products
select pt;
GridView1.DataSource = dt;
GridView1.DataBind();

}
// LINQ : Where
protected void ButtonWhere_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();
var dt = from pt in northwind.Products
where pt.ProductID == 1
select pt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
// LINQ : Foreach
protected void ButtonForeach_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();
var dt = from pt in northwind.Products
select pt;

foreach (var item in dt)
{
Response.Write("product id:" + item.ProductID.ToString() + "<br>");
}
}
// LINQ : Select New
protected void ButtonSelectNew_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();
var dt = from pt in northwind.Products
select new
{
ProductId = pt.ProductID,
ProductName = pt.ProductName
};
GridView1.DataSource = dt;
GridView1.DataBind();
}
// LINQ : Orderby
protected void ButtonOrderBy_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();
var dt = from pt in northwind.Products
orderby pt.ProductID descending
select pt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
// LINQ : Take
protected void ButtonTake_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();
var dt = from pt in northwind.Products
select pt;
GridView1.DataSource = dt.Take(5);
GridView1.DataBind();
}
// LINQ : Skip and Take
protected void ButtonSkipAndTake_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();
var dt = from pt in northwind.Products
select pt;
GridView1.DataSource = dt.Skip(10).Take(5);
GridView1.DataBind();
}
// LINQ : InsertOnSubmit
protected void ButtonInsert_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();

Products newPt = new Products();
newPt.ProductName = "iphone";
newPt.SupplierID = 5;
newPt.CategoryID = 4;
newPt.QuantityPerUnit = "10 - 500 g pkgs.";
newPt.UnitPrice = 25000;
newPt.UnitsInStock = 80;
newPt.UnitsOnOrder = 0;
newPt.ReorderLevel = 0;
newPt.Discontinued = false;
northwind.Products.InsertOnSubmit(newPt);
northwind.SubmitChanges();

var dt = from pt in northwind.Products
orderby pt.ProductID descending
select pt;

GridView1.DataSource = dt;
GridView1.DataBind();
}
// LINQ : Update
protected void ButtonUpdate_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();

Products pt = northwind.Products.Single(currPt => currPt.ProductName == "iphone");
pt.ProductName = "iPhone 3G";
northwind.SubmitChanges();
}
// LINQ : Delete
protected void ButtonDelete_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();

Products pt = northwind.Products.Single(currPt => currPt.ProductName == "iPhone 3G");
northwind.Products.DeleteOnSubmit(pt);
northwind.SubmitChanges();
}
}

90% 症候群