LINQ to SQL.
来源:互联网 发布:淘宝联盟采集工具 编辑:程序博客网 时间:2024/06/14 10:02
http://weblogs.asp.net/rajbk/archive/2010/03.aspx
The following post shows how to write different types of joins in LINQ to SQL. I am using the Northwind database and LINQ to SQL for these examples.
NorthwindDataContext dataContext = new NorthwindDataContext();
Inner Join
var q1 = from c in dataContext.Customers
join o in dataContext.Orders on c.CustomerID equals o.CustomerID
select new
{
c.CustomerID,
c.ContactName,
o.OrderID,
o.OrderDate
};
SELECT [t0].[CustomerID], [t0].[ContactName], [t1].[OrderID], [t1].[OrderDate]
FROM [dbo].[Customers] AS [t0]
INNER JOIN [dbo].[Orders] AS [t1] ON [t0].[CustomerID] = [t1].[CustomerID]
Left Join
var q2 = from c in dataContext.Customers
join o in dataContext.Orders on c.CustomerID equals o.CustomerID into g
from a in g.DefaultIfEmpty()
select new
{
c.CustomerID,
c.ContactName,
a.OrderID,
a.OrderDate
};
SELECT [t0].[CustomerID], [t0].[ContactName], [t1].[OrderID] AS [OrderID], [t1].[OrderDate] AS [OrderDate]
FROM [dbo].[Customers] AS [t0]
LEFT OUTER JOIN [dbo].[Orders] AS [t1] ON [t0].[CustomerID] = [t1].[CustomerID]
Inner Join on multiple
//We mark our anonymous type properties as a and b otherwise
//we get the compiler error "Type inferencce failed in the call to 'Join’
var q3 = from c in dataContext.Customers
join o in dataContext.Orders on new { a = c.CustomerID, b = c.Country } equals new { a = o.CustomerID, b = "USA" }
select new
{
c.CustomerID,
c.ContactName,
o.OrderID,
o.OrderDate
};
SELECT [t0].[CustomerID], [t0].[ContactName], [t1].[OrderID], [t1].[OrderDate]
FROM [dbo].[Customers] AS [t0]
INNER JOIN [dbo].[Orders] AS [t1] ON ([t0].[CustomerID] = [t1].[CustomerID]) AND ([t0].[Country] = @p0)
Inner Join on multiple with ‘OR’ clause
var q4 = from c in dataContext.Customers
from o in dataContext.Orders.Where(a => a.CustomerID == c.CustomerID || c.Country == "USA")
select new
{
c.CustomerID,
c.ContactName,
o.OrderID,
o.OrderDate
};
SELECT [t0].[CustomerID], [t0].[ContactName], [t1].[OrderID], [t1].[OrderDate]
FROM [dbo].[Customers] AS [t0], [dbo].[Orders] AS [t1]
WHERE ([t1].[CustomerID] = [t0].[CustomerID]) OR ([t0].[Country] = @p0)
Left Join on multiple with ‘OR’ clause
var q5 = from c in dataContext.Customers
from o in dataContext.Orders.Where(a => a.CustomerID == c.CustomerID || c.Country == "USA").DefaultIfEmpty()
select new
{
c.CustomerID,
c.ContactName,
o.OrderID,
o.OrderDate
};
SELECT [t0].[CustomerID], [t0].[ContactName], [t1].[OrderID] AS [OrderID], [t1].[OrderDate] AS [OrderDate]
FROM [dbo].[Customers] AS [t0]
LEFT OUTER JOIN [dbo].[Orders] AS [t1] ON ([t1].[CustomerID] = [t0].[CustomerID]) OR ([t0].[Country] = @p0)
- linq,linq to sql
- Linq初探--Linq to SQL
- LINQ to SQL 学习
- 什么是Linq to sql
- 使用LINQ to SQL
- 深入LINQ to SQL
- Linq To Sql注意事项
- Linq to SQL:基础知识
- LINQ to SQL实例
- 使用LINQ to SQL
- LINQ to SQL语句
- Linq to sql(一)
- LINQ to SQL语句
- LINQ TO SQL--insert
- LINQ TO SQL--delete
- LINQ TO SQL--update
- LINQ TO SQL--select
- Linq to SQL 资源
- 腾讯微博Android客户端开发——算法、编码、辅助方法编写
- Ubuntu 上安装 ibus云拼音输入法
- oc文件组织
- 写给新手程序员的一封信
- android应用开发环境搭建--windows
- LINQ to SQL.
- precheck for the grid control installation
- #paragma预处理命令疑问
- 谈一谈网络编程学习经验(06-08更新)
- Discuz!安装后的雏形,初体验
- javascript 密码验证
- java最常见面试题
- U盘开发之USB协议
- ie证书问题