LINQ to SQL 查询数据库和使用存储过程

如下列出查询表达式,常用操作符,与SQL语句对应。

LINQ to SQL 查询数据库和使用存储过程

where  关键字的使用

public void MyWhere()
       {
           NorthwindDataContext dc = new NorthwindDataContext();
           //查询产品名称以L开头的记录
           var query = from p in dc.Products
                       where p.ProductName.StartsWith("L")
                       select p;

           //两个where过滤两个条件,第二个where会在第一个where基础上进行第二次条件谓词
           var query1 = from p in dc.Products
                        where p.ProductName.StartsWith("L")
                        where p.ProductName.EndsWith("i")
                        select p;

           foreach (Products item in query1)
           {
               Response.Write(item.ProductID + " | " + item.ProductName + "<br />");
           }

       }

 

join 连接操作

拖动数据库的Orders表到设计器中来,结果如下

LINQ to SQL 查询数据库和使用存储过程

设计器会根据表在数据库中设定的依赖关系,生成表对象之间的依赖关系,查看表关系,可以打开SQL Server 管理器,打开数据库右键点击表,选择查看依赖关系。

LINQ to SQL 查询数据库和使用存储过程

可以看到Order依赖Customer

LINQ to SQL 查询数据库和使用存储过程

如果你想给表添加或者管理一个FK的话可以看下图,如果想添加就右击、键目录、添加键,如下图

LINQ to SQL 查询数据库和使用存储过程

再点击表和列规范后面的按钮

LINQ to SQL 查询数据库和使用存储过程

选择主表和列对应本表那个列(外键)确定,点保存,刷新一下就可以看到了

代码:

public void MyJoin()
        {
            // PERFORMING JOINS

            NorthwindDataContext dc = new NorthwindDataContext();
            dc.Log = Console.Out;

            var query = from c in dc.Customers
                        join o in dc.Orders on c.CustomerID equals o.CustomerID
                        orderby c.CustomerID
                        select new
                        {
                            c.CustomerID,
                            c.CompanyName,
                            c.Country,
                            o.OrderID,
                            o.OrderDate
                        };

            foreach (var item in query)
            {
                Response.Write(item.CustomerID + " | " + item.CompanyName
                     + " | " + item.Country + " | " + item.OrderID
                      + " | " + item.OrderDate + "<br />");
            }
        
        }

 

组合数据项Group

拖动Categories 表到设计器,如下图

LINQ to SQL 查询数据库和使用存储过程

代码如下:

public void MyGroup()
        {
            NorthwindDataContext dc = new NorthwindDataContext();

            var query = from p in dc.Products //from谓词
                        orderby p.Categories.CategoryName ascending //排序
                        group p by p.Categories.CategoryName into g //以名字进行分组,导入到新的集合,Key是分组的CategoryName当前名,Value 是分组的Products对象
                        select new { Categories = g.Key, Products = g }; //建立一个匿名对象,分别,将Key,和Value放进去

            foreach (var item in query)
            {
                Response.Write("分组Key,CategoryName:" + item.Categories + "<br />");

                foreach (var innerItem in item.Products)
                {
                    Response.Write( innerItem.ProductName + "<br />");
                }

                Response.Write("<p />");
            }
        }

LINQ 调用存储过程

设计器的右侧是用来显示和定义存储过程的地方,可以把存储过程拖进来,在使用的时候会变成LINQ语法DataContext对象中的一个方法

LINQ to SQL 查询数据库和使用存储过程

代码如下:

public void MyProcedure()
        {
            NorthwindDataContext dc = new NorthwindDataContext();
            //ISingleResult< Ten_Most_Expensive_Products_个结果 > result = dc.Ten_Most_Expensive_Products();    //单个返回序列结果
            //返回结果类型和怪,出现了中文,这是我直接拖动自动生成的,我们可以替换,这个_个结果在Northwind.designer.cs 文件中
            ISingleResult<Ten_Most_Expensive_Products_Result> result = dc.Ten_Most_Expensive_Products(); 
            foreach (var item in result)
            {
                Response.Write(item.TenMostExpensiveProducts + " | " + item.UnitPrice + "<br />");
                // 属性名称,对应 存储过程中的 结果列名称,SELECT Products.ProductName AS TenMostExpensiveProducts, Products.UnitPrice
            }
        
        
        }
替换方法:

LINQ to SQL 查询数据库和使用存储过程