Lambda式3


強いタイプのDataContext
NorthwindDataContext.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using System.Data;

namespace DannyWeb
{
    public class NorthwindDataContext : DataContext
    {
        public Table<Customer> Customers;
        public NorthwindDataContext(IDbConnection connection) : base(connection) { }
        public NorthwindDataContext(string connection) : base(connection) { }
    }
}

ページを表示
Default2.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DannyWeb;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        NorthwindDataContext ctx = new NorthwindDataContext("server=.;database=Northwind;uid=sa;pwd=");
        GridView1.DataSource = from c in ctx.Customers
                               where c.CustomerID.StartsWith("A")
                               select new {  ? ¨ªID = c.CustomerID,  ? ¨ª ? = c.Name,  ? ºD = c.City };
        GridView1.DataBind();
    }
}

結果を表示:
2011-6-2 13:18 danny
ログを表示:
Default2.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DannyWeb;
using System.IO;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        NorthwindDataContext ctx = new NorthwindDataContext("server=.;database=Northwind;uid=sa;pwd=");
        StreamWriter sw = new StreamWriter(Server.MapPath("log.txt"), true);
        ctx.Log = sw;
        GridView1.DataSource = from c in ctx.Customers
                               where c.CustomerID.StartsWith("A")
                               select new {  ? ¨ªID = c.CustomerID,  ? ¨ª ? = c.Name,  ? ºD = c.City };
        GridView1.DataBind();
        sw.Close();
    }
}

ログ#ログ#
log.txt

SELECT [t0].[CustomerID] AS [  ID], [t0].[ContactName] AS [   ], [t0].[City] AS [  ]
FROM [Customers] AS [t0]
WHERE [t0].[CustomerID] LIKE @p0
-- @p0: Input NVarChar (Size = 2; Prec = 0; Scale = 0) [A%]
-- Context: SqlProvider(Sql2000) Model: AttributedMetaModel Build: 4.0.30319.1

2011-6-2 13:24 danny