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如下:

执行结果:


页面效果:

示例二:

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
};

 

posted @   skybirdzw  阅读(1905)  评论(1编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示