Linq to sql之left join运用示例

示例一:

var l= from a in cardsBll.GetCards()
	  join b in usersBll.GetAllUsers()
	  on a.CardSn equals b.CardSn into temp
	  where a.CardSn.Contains(key)
	  from t in temp.DefaultIfEmpty()
	  select new CardsAndUsersView
	  {
		  Card_Id = a.Id,
		  CardPw = a.CardPw,
		  Expries = a.Expries,
		  IsEnabled = a.IsEnabled,
		  CardSn = a.CardSn,
		  CreateDate = a.CreateDate,
		  ModifyDate = a.ModifyDate,
		  User_Id = t==null?0:t.Id,
		  Province = t == null ? "" : t.Province,
		  City = t == null ? "" :t.City,
		  AreaOrCounty = t == null ? "" : t.AreaOrCounty,
		  AddressInfo = t == null ? "" : t.AddressInfo
	  };

生成的SQL如下:

exec sp_executesql N‘SELECT TOP (20)
[Project1].[Id] AS [Id],
[Project1].[CardPw] AS [CardPw],
[Project1].[Expries] AS [Expries],
[Project1].[IsEnabled] AS [IsEnabled],
[Project1].[CardSn] AS [CardSn],
[Project1].[CreateDate] AS [CreateDate],
[Project1].[ModifyDate] AS [ModifyDate],
[Project1].[C1] AS [C1],
[Project1].[C2] AS [C2],
[Project1].[C3] AS [C3],
[Project1].[C4] AS [C4],
[Project1].[C5] AS [C5]
FROM ( SELECT [Project1].[Id] AS [Id], [Project1].[CardSn] AS [CardSn], [Project1].[CardPw] AS [CardPw], [Project1].[IsEnabled] AS [IsEnabled], [Project1].[Expries] AS [Expries], [Project1].[CreateDate] AS [CreateDate], [Project1].[ModifyDate] AS [ModifyDate], [Project1].[C1] AS [C1], [Project1].[C2] AS [C2], [Project1].[C3] AS [C3], [Project1].[C4] AS [C4], [Project1].[C5] AS [C5], row_number() OVER (ORDER BY [Project1].[Id] ASC) AS [row_number]
	FROM ( SELECT
		[Extent1].[Id] AS [Id],
		[Extent1].[CardSn] AS [CardSn],
		[Extent1].[CardPw] AS [CardPw],
		[Extent1].[IsEnabled] AS [IsEnabled],
		[Extent1].[Expries] AS [Expries],
		[Extent1].[CreateDate] AS [CreateDate],
		[Extent1].[ModifyDate] AS [ModifyDate],
		CASE WHEN ([Extent2].[Id] IS NULL) THEN 0 ELSE [Extent2].[Id] END AS [C1],
		CASE WHEN ([Extent2].[Id] IS NULL) THEN N‘‘‘‘ ELSE [Extent2].[Province] END AS [C2],
		CASE WHEN ([Extent2].[Id] IS NULL) THEN N‘‘‘‘ ELSE [Extent2].[City] END AS [C3],
		CASE WHEN ([Extent2].[Id] IS NULL) THEN N‘‘‘‘ ELSE [Extent2].[AreaOrCounty] END AS [C4],
		CASE WHEN ([Extent2].[Id] IS NULL) THEN N‘‘‘‘ ELSE [Extent2].[AddressInfo] END AS [C5]
		FROM  [dbo].[Cards] AS [Extent1]
		LEFT OUTER JOIN [dbo].[UserInfo] AS [Extent2] ON ([Extent1].[CardSn] = [Extent2].[CardSn]) OR (([Extent1].[CardSn] IS NULL) AND ([Extent2].[CardSn] IS NULL))
		WHERE (1 = [Extent1].[IsEnabled]) AND ([Extent1].[CardSn] LIKE @p__linq__0 ESCAPE N‘‘~‘‘)
	)  AS [Project1]
)  AS [Project1]
WHERE [Project1].[row_number] > 0
ORDER BY [Project1].[Id] ASC‘,N‘@p__linq__0 nvarchar(4000)‘,@p__linq__0=N‘%%‘

执行结果:

页面效果:

示例二:

DataClasses1DataContext db = new DataClasses1DataContext();
var leftJoinSql = from student in db.Student
join book in db.Book on student.ID equals book.StudentID into temp
from tt in temp.DefaultIfEmpty()
select new
{
	sname= student.Name,
	bname = tt==null?"":tt.Name
};

原文地址:https://www.cnblogs.com/zhaow/p/9796526.html

时间: 2024-10-20 09:20:40

Linq to sql之left join运用示例的相关文章

LINQ TO SQL 中的join(转帖)

http://www.cnblogs.com/ASPNET2008/archive/2008/12/21/1358152.html join对于喜欢写SQL的朋友来说还是比较实用,也比较容易接受的东西.在LINQ TO SQL中,写多表查询,同样可以写join,只是它有它自己的语法要求而已,语义都是一样的,下面我来讲下LINQ TO SQL中的join最基本的形式:都是最简单的,当然还有其它方面的内容,如:怎样加上过滤条件,如何分组,如何排序等等,为了单纯说join的用法,这里就简化下. Cod

Linq To SQL LEFT OUTER JOIN (Left Join)

SQL: SELECT [t0].[ProductName], [t1].[TotalPrice] AS [TotalPrice] FROM [Product] AS [t0] LEFT OUTER JOIN [OrderDetail] AS [t1] ON [t0].[ProductID] = [t1].[ProductID] Linq Query: from p in Products join od in OrderDetails on p.ProductID equals od.Prod

在Linq to sql 和 Entity framework 中使用lambda表达式实现left join

我们知道lambda表达式在Linq to sql 和 Entity framework 中使用join函数可以实现inner join,那么怎么才能在lambda表达式中实现left join呢?秘诀就是在join后面加上一个函数DefaultIfEmpty函数,实际上这个函数在linq中貌似也只有将inner join转换为left join的作用,示例如下 var joinResult = DB.Table1s.Join(DB.Table2s, a => a.id, b => b.id,

LINQ to SQL语句(4)之Join

1.一对多关系(1 to Many): var q = from c in db.Customers from o in c.Orders where c.City == "London" select o; 语句描述:Customers与Orders是一对多关系.即Orders在Customers类中以 EntitySet形式出现.所以第二个from是从c.Orders而不是db.Orders里进行筛选 .这个例子在From子句中使用外键导航选择伦敦客户的所有订单. var q =

LINQ to SQL Count/Sum/Min/Max/Avg Join

public class Linq { MXSICEDataContext Db = new MXSICEDataContext(); // LINQ to SQL // Count/Sum/Min/Max/Avg // Count public void Count() { // 说明:返回集合中的元素个数,返回 INT 类型:不延迟.生成 SQL 语句为: SELECT COUNT(*) FROM //简单形式 性能差 var count = Db.MXSMemeber.Count(); /

LINQ系列:LINQ to SQL Join连接

1. 一对多 var expr = context.Products .Where(p => p.Category.CategoryName == "LINQ to SQL" && p.UnitPrice > 10m) .Select(p => new { p.ProductID, p.ProductName }); var expr = from p in context.Products where p.Category.CategoryName

LINQ中的连接(join)用法示例

Linq中连接主要有组连接.内连接.左外连接.交叉连接四种.各个用法如下. 1. 组连接 组连接是与分组查询是一样的.即根据分组得到结果. 如下例,根据publisther分组得到结果. 使用组连接的查询语句如下: //使用组连接 var GroupQuery = from publisher in SampleData.Publishers join book in SampleData.Books on publisher equals book.Publisher into publish

LINQ to SQL系列四 使用inner join,outer join

先看一个最简单的inner join,在读取Student表时inner join Class表取的对应的Class信息: static void Main(string[] args) { using (var writer = new StreamWriter(WatchSqlPath, false, Encoding.UTF8)) { using (DbAppDataContext db = new DbAppDataContext()) { db.Log = writer; //inne

LINQ To SQL 语法及实例大全

LINQ to SQL语句(1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句. Where操作包括3种形式,分别为简单形式.关系条件形式.First()形式.下面分别用实例举例下: 1.简单形式: 例如:使用where筛选在伦敦的客户 var q = from c in db.Customers where c.City == "London" select c