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();
}
}

2008/10/18

字串比對上的眉角



if (e.Row.Cell[0].Text.Equals("mytest"))

...

改成



if ("mytest".Equals(e.Row.Cell[0].Text))

...

可避免因為e.Row.Cell[0].Text如果是空值而造成程式exception.

2008/10/13

jquery: 取出指定form內的所有select物件

在myform裡取出所有select name 開頭是'att'的值:

$("form[name='myform'] select[name^='att']").each(function(){
alert($(this).val());
});

判斷瀏覽器是否為Google Chrome的二種方法

1. 使用jquery,但要先加入一個jquery plugin, 可以以下連結下載:

jQuery Browser Plugin

下載後,include到你的html裡,然後直接下指令即可:


if ($.browser.name == "chrome")
alert("google chrome!");


2. 直接判斷navigator.userAgent裡的文字, 請參考:Detecting Google Chrome Using Javascript


var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

2008/09/26

WCF WebService how to use Server.MapPath function. 如何在WCF WebService裡使用Server.MapPath

為何預設的WCF WebService沒有enable ASP.NET相容模式呢?
因為WCF是微軟新推出的分散式架構解決方案,有些client端的應用程式並不一定是web,
所以也就沒有必要先把ASP.NET相容模式打開,保留了一些彈性。

若開啟ASP.NET相容模式後:

  • WCF 行為就會在下列 ASP.NET 功能上與 ASMX 行為一致:
  • 在 ASP.NET 相容性模式中執行的 HttpContext: WCF 服務可以存取 Current 與其關聯狀態。
  • 檔案架構授權:在 ASP.NET 相容性模式中執行的 WCF 服務可以將檔案系統存取控制清單 (ACL) 附加至服務的 .svc 檔中,以保護自身的安全。
  • 可設定的 URL 授權:在 ASP.NET 相容性模式中執行 WCF 服務時,會強制執行 WCF 要求的 ASP.NET 的 URL 授權規則。
  • HttpModuleCollection 擴充性:由於在 ASP.NET 相容性模式中執行的 WCF 服務會充分參與 ASP.NET HTTP 要求的生命週期,任何透過 HTTP 管線設定的 HTTP 模組都能夠在叫用服務前/後在 WCF 要求上運作。
  • ASP.NET 模擬:如果已經針對應用程式啟用了 ASP.NET 模擬,則透過 ASP.NET 模擬執行緒目前的身分識別來執行的 WCF 服務可能會與 IIS 處理序身分識別不同。 如果同時針對特定的服務作業啟用了 ASP.NET 模擬與 WCF 模擬,則服務實作最終將會透過從 WCF 取得的身分識別來執行。
  • Server.MapPath無法使用是因為在WCF Server端中,HttpContext.Current會是null,所以無法使用。

設定步驟如下:

1. 開啟aspNetCompatibilityEnabled=true



在web.config裡會看到下面這段:


<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />


2. 在Implemente的class加入AspNetCompatibilityRequirements

ITestService.cs :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

[ServiceContract]
public interface ITestService
{
[OperationContract]
void DoWork();

[OperationContract]
string GetLogFilePath();
}


TestService.cs :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using System.Web;

[ServiceBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class TestService : ITestService
{
public void DoWork()
{
}

public string GetLogFilePath()
{
string logFile = "~/res/iislog/100/ex07021023.log.gz";
return HttpContext.Current.Server.MapPath(logFile);
}
}


這樣在client就不會有exception了....

參考資料:


象印不鏽鋼真空保溫瓶(SF-CC20)

軟體工程的重要的指標