查询自动生成的EF联结表

时间:2012-11-06 10:16:18

标签: c# asp.net-mvc linq entity-framework ef-code-first

我有两个模型,Benefit和SchemeName

福利 -

[Key]
public int BenefitID { get; set; }
public string BenefitName { get; set; }
public string BenefitDescription { get; set; }
public virtual ICollection<SchemeName> SchemeNames { get; set; }

SchemeName

[Key]
public int SchemeNameID { get; set; }
public string Name { get; set; }
public virtual ICollection<Benefit> Benefits { get; set; }

这在数据库的Benefits,SchemeNames和一个名为SchemeNameBenefits的连接表中创建了三个表。

我正在尝试填充仅包含与某个Benefit关联的SchemeNames的droplownlist,但我不知道如何执行此操作,我可以在代码中引用连接表吗?

我从以下开始(返回所有SchemeNames)

private void PopulatePensionSchemeName(object selectedPensionSchemeName = null)
        {
            var schemeNameQuery = from d in db.SchemeNames
                                  orderby d.SchemeNameID
                                  select d;

            ViewBag.PensionSchemeNameID = new SelectList(schemeNameQuery, "SchemeNameID", "Name", selectedPensionSchemeName);
        }

但我不确定如何添加此条款。有什么指针吗?

3 个答案:

答案 0 :(得分:2)

您需要SchemeNames所需的Benefit对象的键。您可能正在寻找的查询是: -

var benefitId = // However you get your benefit Id

var schemaNameQuery = from b in db.Benefits
                      from s in b.SchemeNames
                      where b.BenefitId == benefitId
                      select s;

或者在扩展方法语法中: -

var schemaNameQuery = db.Benefits.Where(b.BenefitId == benefitId)
                                 .SelectMany(b => b.SchemeNames);

产生以下SQL: -

SELECT ...
FROM [dbo].[SchemeNameBenefits] AS [Extent1]
INNER JOIN [dbo].[SchemeNames] AS [Extent2]
  ON [Extent1].[SchemeName_Id] = [Extent2].[SchemeNameId]
WHERE [Extent1].[Benefit_Id] = @p__linq__0

或者您可以使用: -

var benefitId = // However you get your benefit Id

var schemeNameQuery = from d in db.SchemeNames
                      where d.Benefits.Any(x => x.Id == benefitId)
                      orderby d.SchemeNameId
                      select d;

这会生成以下SQL: -

SELECT ...
FROM ( SELECT ... FROM [dbo].[SchemeNames] AS [Extent1]
       WHERE EXISTS (SELECT 1 AS [C1]
                     FROM [dbo].[SchemeNameBenefits] AS [Extent2]
                     WHERE ([Extent1].[SchemeNameId] = [Extent2].[SchemeName_Id])
                     AND ([Extent2].[Benefit_Id] = @p__linq__0)))
AS ...
ORDER BY [Project2].[Id] ASC

请注意,在这两种情况下,生成的SQL都会引用您的联结表,即使它不是您的EF模型的一部分。

如果您已经拥有了Benefit对象,当然,您可以通过以下方式更简单地获取其SchemeNames: -

var schemeNameQuery = benefit.SchemeNames;

答案 1 :(得分:1)

我会通过使用连接表来完成此操作。连接表是您的连接表。它将包含两个外键,SchemeNameID&amp; BenefitID。

查看本网站了解更多有关接线表的信息:

http://megocode3.wordpress.com/2008/01/04/understanding-a-sql-junction-table/

它帮助了我很多。

答案 2 :(得分:1)

[免责声明]有条件存在或添加SchemeNameBenefits模型,以下内容应该有效。

因此,似乎SchemeNameBenefits表是多对多映射,在这种情况下使用收益ID来获取方案ID的集合

var schemeIds = db.SchemeNamesBenefits.Where(map => map.BenefitID == id)
                                      .Select(map => map.SchemeNameID).ToArray();

然后撤回这些方案ID的所有方案名称信息

var result = db.SchemeNames.Where(scheme => schemeIds.Contains(scheme.SchemeNameID))
                           .OrderBy(scheme => scheme.SchemeNameId)
                           .Select(scheme => scheme.Name).ToArray();

或在一个查询中

 var result = db.SchemeNamesBenefits.Where(map => map.BenefitID == id)
                                    .SelectMany(map => db.SchemeNames
                                                         .Where(scheme => map.SchemeNameID == scheme.SchemeNameID)
                                                         .OrderBy(scheme => scheme.SchemeNameId)
                                                         .Select(scheme => scheme.Name)
                                                         .AsEnumerable())
                                    .ToArray()