技术开发 频道

SQL Server 2008 LINQ to Entity

编写LINQ to Entity代码

1. 在Solution Explorer面板中,双击Default.aspx文件,在打开的页面编辑器中,点击左下角的Design以切换到设计界面。

2. 查看Toolbox面板,展开Data,将里面的GridView控件拖放到页面当中。

3. 在Solution Explorer面板中,右键单击Default.aspx文件,选择View Code以查看C#源代码。

4. 在代码上方,添加一个命名空间的using:
using Northwind;

5. 在Page_Load方法体中,输入下面的代码进行实体数据模型的访问,并将数据绑定到前面的GridView控件上:

NorthwindEntities entities = new NorthwindEntities();
        var ordersOf1996
= from order in entities.Orders
                          
where order.OrderDate < new DateTime(1997, 1, 1)
                           select
new
                           {
                               order.OrderID,
                               order.OrderDate,
                               order.ShipCity,
                               order.Customers.CompanyName
                           };
        
this.GridView1.DataSource = ordersOf1996.Take(20);
        
this.GridView1.DataBind();

6. 点击键盘上的F5键,以运行应用程序。

7. 在弹出的对话框中,点击OK,如果还有对话框弹出,点击Yes。

8. 这时,便可以看到对于实体数据模型的查询结果。

9. 关闭Visual Studio 2008。

10. 关闭Virtual PC,不保存任何更改。
 

0
相关文章