【问题标题】:I have never used JOIN. When is proper time?我从未使用过JOIN。什么时候是合适的时间?
【发布时间】:2017-01-25 12:01:07
【问题描述】:

我从来没有用过JOIN(INNER, OUTER),我也不知道什么时候是最好的场景:

这里有两个ActionResults 的示例,它们使用2 或3 个查询来获取对象,使用JOIN 不是更好吗?

第一个例子:

public ActionResult JobTags(int id)
{
   var jobTagsList = (from j in db.JobTags
                           where j.JobID == id
                           select j.TagID).ToList();

   var tags = (from j in db.Tags
                    where jobTagsList.Contains(j.ID.ToString())
                    select j).ToList();

   return View(tags);
}

我可以只JOIN这两个表并选择最后的那个j吗?

第二个例子:

public ActionResult ImageListWhoApp(int id)
{
    //We get here the ID from Job page using dbo.Jobs
    var userIdList = (from j in db.Jobs
                           where j.ID == id
                           select j.ID.ToString()).ToList();

    //We get here who applied at this job using dbo.AppliedJobs
    var appJobIdList = (from j in db.AppliedJobs
                    where userIdList.Contains(j.JobID.ToString())
                    select j.UserID).ToList();

    //Finally we get here the avatars of the user who applied at the job
    //We are using this as a hyperlink to user profile.
    var appUserImage = (from j in db.Images
                            where appJobIdList.Contains(j.UserID.ToString())
                            select j).ToList();

    return View(appUserImage);
}

这种方法是不是越来越荒谬了?或者以这种方式做这样的事情是正常的?如何从这 3 个 SQL 语句中生成 JOIN?可能吗?这是更好的方法吗?

感谢您的宝贵时间!

【问题讨论】:

  • 您的模型类中没有导航属性吗?它们是使联接几乎没有必要的原因。
  • 我也是,因为与 T-SQL 中的 JOIN 相比,LINQ 中的 JOIN 冗长且有限。
  • 如果您使用 EF 并具有导航属性,则通常不需要 JOIN。但是,如果您有两个包含要组合的对象的列表,那么这很有意义。
  • @qxg:详细程度是高度基于意见的,对于 LINQ 中.Join() 的限制,您应该查看.GroupJoin()
  • 是的,我完全忘记了导航属性,在这种情况下,无论如何我都不需要JOIN,这只是在问自己为什么我现在根本没有使用JOIN。但是一次返回两个列表是有道理的,我从来不需要那个。

标签: asp.net-mvc linq join


【解决方案1】:

您不需要连接。您可以使用导航属性:

var tagsQry =
    from tag in db.Tags
    where tag.JobTag.JobID == id
    select tag;

var userImageQry =
    from img in db.Images
    from appJob in db.AppliedJobs
    where (img.UserID == appJob.UserID) && (appJob.Job.ID == id)
    select img;

即使没有导航属性,也不需要连接:

var tagsQry =
    from tag in db.Tags
    from jobTag in sb.JobTags
    where (jobTag.JobID == id) && (tag.ID == jobTag.TagID)
    select tag;

var userImageQry =
    from img in db.Images
    from appJob in db.AppliedJobs
    from job in db.Jobs
    where (img.UserID == appJob.UserID) && (appJob.JobID == job.ID) && (job.ID == id)
    select img;

但是,如果您喜欢这种语法,您可以使用连接。 DB端查询执行计划将完全相同:

var tagsQry =
    from tag in db.Tags
    join jobTag in sb.JobTags on tag.ID equals jobTag.TagID
    where (jobTag.JobID == id)
    select tag;

var userImageQry =
    from appJob in db.AppliedJobs
    join img in db.Images on appJob.UserID equals img.UserID
    join job in db.Jobs on appJob.JobID equals job.ID
    where (job.ID == id)
    select img;

在第二个示例中,如果您在 AppliedJobs.JobID 上没有外键约束,则只需查询(或连接)到 Jobs。如果有,可以直接比较AppliedJobs.JobIDid

【讨论】:

  • 实际上我完全忘记了导航属性,但无论哪种方式,我的困境都是关于JOIN,我为什么要使用或不使用,因为经过长时间学习MVC,我认为它没有任何意义。
猜你喜欢
  • 1970-01-01
  • 2011-09-13
  • 1970-01-01
  • 2010-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多