zoukankan      html  css  js  c++  java
  • linq 之左连接

    List<ArticleModel> articleList = articleRepository.GetAllArticle();
    List<UsersModel> userList = usersRepository.GetAllUsers();

    //用户表左连接文章表
    var usersLeftJoin = (from u in userList
    join a in articleList
    on u.Id equals a.Author into users
    from us in users.DefaultIfEmpty()
    select new
    {
    author = us == null ? "" : us.Author.ToString(),
    name = u.Name.Trim()
    }).ToList();

    //要避免这种写法,这种写法查出来的数据实际上相当于连接
    var usersLeftJoin1 = (from u in userList
    join a in articleList
    on u.Id equals a.Author into users
    from us in users
    select new
    {
    author = us.Author,
    name = u.Name.Trim()
    }).ToList();


    //文章表左连接用户表
    var articleLeftJoin = (from a in articleList
    join u in userList
    on a.Author equals u.Id into users
    from us in users.DefaultIfEmpty()
    select new
    {
    title = a.Title.Trim(),
    name = us == null ? "" : us.Name.Trim(),
    author = a.Author
    }).ToList();

    //要避免这种写法,这种写法查出来的数据实际上相当于连接
    var articleLeftJoin1 = (from a in articleList
    join u in userList
    on a.Author equals u.Id into users
    from us in users
    select new
    {
    name = us.Name.Trim(),
    author = a.Author
    }).ToList();

    //用户表连接文章表
    var result = (from a in articleList
    from u in userList
    where a.Author == u.Id
    select new
    {
    name = u.Name,
    author = a.Author
    }).ToList();

  • 相关阅读:
    10.28
    10.25
    10.21
    移动第七次作业
    移动第六次作业
    移动第五次作业
    移动第四次作业
    移动第3次作业
    移动第二次作业
    移动第一次作业
  • 原文地址:https://www.cnblogs.com/ushou/p/3408014.html
Copyright © 2011-2022 走看看