LINQ to SQL中的SELECT查询用于获取表中的所有记录或行。LINQ to SQL SELECT查询可以用来过滤带有WHERE子句的表的记录。
在这里,无涯教程还可以根据需要使用LINQ to SQL SELECT查询执行多个操作,如分组、连接等。
以下是LINQ to SQL Select查询的语法。
EmployeeDBDataContext db = new EmployeeDBDataContext(); var result = from ed in db.EmployeeDetails select new { EmployeeName = ed.EmpName, Location = ed.Location };
使用上述语法,无涯教程使用 LINQ to SQL Select Query 从“EmployeeDetails”表中获取记录。
首先开始实施LINQ to SQL内联接之前,需要创建一个包含所需表的数据库。将这些表LINQ映射到SQL文件(.dbml)。若要了解映射过程,请单击链接LINQ to SQL。
创建所需的表并将其映射到.dbml文件后,现在希望在应用程序中显示数据。为此,必须右键单击application->select add->new item->Select Web Form-> 将名称指定为Default.aspx,然后单击确定按钮。
现在打开页面Default.aspx,并编写如下所示的代码。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> </head> <body> <form id="form2" runat="server"> <div> <asp:GridView ID="GvDetail" runat="server"></asp:GridView> </div> </form> </body> </html>
现在打开文件背后的代码,编写代码,如下图所示:
using System; using System.Collections.Generic; using System. Linq; using System. Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { EmployeeDBDataContext db1 = new EmployeeDBDataContext(); var result = from ed in db.EmployeeDetails select new { EmployeeName = ed.EmpName, Location = ed.Location, Gender = ed.Gender }; gvDetails.DataSource = result; gvDetails.DataBind(); } }
从上面的示例中,无涯教程使用 LINQ to SQL 选择查询从“EmployeeDetails”表中获取详细信息。
输出
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)