【问题标题】:Transform SQL query to Linq left join clause incorrect将 SQL 查询转换为 Linq 左连接子句不正确
【发布时间】:2016-04-18 08:50:10
【问题描述】:

我正在尝试选择 Unlock 表中不存在的 MatchingObjects,我的 SQL 查询如下:

select a.* from MatchingObjects a 
    left join Unlocks b
        on a.ObjectCategoryId = b.ObjectCategoryId
    left join Members c
        on b.StudentId = c.Id
            and b.StudentId = @studentId
where  b.ObjectCategoryId is null
    and c.id is null
order by a.ObjectCategoryId

还有一个 LINQ 查询

var query = (from d in db.ObjectCategories
                     join a in db.MatchingObjects on d.Id equals a.ObjectCategoryId into grp3
                     join b in db.Unlocks
                         on d.Id equals b.ObjectCategoryId into grp1
                     from m in grp1.DefaultIfEmpty()
                     join c in db.Members
                         on m.StudentId equals c.Id into grp2
                     from n in grp2.DefaultIfEmpty()
                     where m.ObjectCategoryId == null
                     && n.Id == null
                     orderby d.Id).AsEnumerable()
                     ;

但是,LINQ 查询显示的结果与我在 SQL 查询中想要的结果不同。你们能告诉我应该在我的 LINQ 查询中更改什么吗?

这是模型:

【问题讨论】:

  • 对我来说这不是同一个查询!!你必须从同一张桌子开始

标签: c# sql sql-server linq


【解决方案1】:

您可以更好地使用以下工具:

一个 SQL-> LINQ 转换器..

  1. http://www.sqltolinq.com

  2. http://www.linqpad.net/

【讨论】:

  • 感谢 Vignesh Kumar 的建议,我稍后再试看看是否符合我的预期结果,但是我在工作场所的电脑无法安装外部软件
【解决方案2】:

试试这个

var query = from m in db.MatchingObjects.Where(w => w.ObjectCategoryId == null)
                            join u in db.Unlocks.Where(w => w.studentId == @studentId)
                                on m.ObjectCategoryId equals u.ObjectCategoryId into joinedU
                            from ju in joinedU.DefaultIfEmpty()
                            join m in db.Members.Where(w => w.id == null)
                                on ju.StudentId equals m.Id into joinedM
                            from jm in joinedM.DefaultIfEmpty()
                            select m;

但是你的要求有点奇怪。您通过 ObjectCategoryId 进行连接,并在 Where 子句中放置 ObjectCategoryId == null!!!

【讨论】:

  • 嗨,Amine,感谢您的回复,但很抱歉这是不正确的
  • == null 是左连接所必需的
【解决方案3】:

抱歉,Sql 和 LINQ 查询在表选择方面有点不同。对此我很抱歉,因为我在发布问题时尝试了不同的 Linq,但主要问题在于 join 子句

(from d in db.ObjectCategories
                     join a in db.MatchingObjects
                        on d.Id equals a.ObjectCategoryId into grp3
                     join b in db.Unlocks
                        on d.Id equals b.ObjectCategoryId into grp1
                     from m in grp1.DefaultIfEmpty()
                     join c in db.Members 
                        on m.StudentId equals studentId into grp2
                     from n in grp2.DefaultIfEmpty()
                     where m.ObjectCategoryId == null
                     where n.Id == null
                     orderby d.Id).AsEnumerable()


/* this is the correct one */
join c in db.Members 
on m.StudentId equals studentId into grp2

/* the below was the original incorrect join clause*/
join c in db.Members
on m.StudentId equals c.Id into grp2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 2012-03-25
    • 1970-01-01
    相关资源
    最近更新 更多