可以实现条件连接子句 - JOIN ON condition1 OR condition2?

时间:2015-07-08 18:44:12

标签: c# sql-server linq entity-framework entity-framework-6

是否可以在Entity Framework 6中实现条件连接子句?具体来说,INNER JOIN ON(布尔条件1)或(布尔条件2)。

以下代码有效,但会调用数据库两次。是否可以将其合并为一个电话?

有一个外键关系将FirmFeatures.FeatureId绑定到Nullable FirmParameters.FeatureId

var dbContext = new MyEntities();
var feature = dbContext.FirmFeatures
    .Where(f => f.FeatureId == featureId)
    .First();

var parameters = dbContext.FirmParameters.AsQueryable();

parameters = feature.IsDbTable 
    ? parameters.Where(p => p.FeatureId == null) 
    : parameters.Where(p => p.FeatureId == featureId);

var list = parameters.ToList()

SQL调用看起来像:

SELECT feature.*, parameter.*
FROM [FirmFeature] AS feature
INNER JOIN [FirmParameter] AS parameter 
    ON (feature.IsDbTable = 0 AND feature.FeatureId = parameter.FeatureId) OR (feature.IsDbTable = 1 AND parameter.FeatureId IS NULL)
WHERE feature.[FeatureId] = 3

首先使用此杠杆数据库模型。

我是实体框架的新手。

Edit2:我希望同时从数据库中加载一个feature对象和一个参数对象。

编辑:根据要求,以下是模型:

{
    public FirmFeature()
    { this.FirmParameters = new HashSet<FirmParameter>(); }

    public byte FeatureId { get; set; }
    public bool IsDbTable { get; set; }
    ...
    public virtual ICollection<FirmParameter> FirmParameters { get; set; }
}

public partial class FirmParameter
{
    public byte ParameterId { get; set; }
    public Nullable<byte> FeatureId { get; set; }
    ...    
    public virtual FirmFeature FirmFeature { get; set; }
    public virtual FirmParameter FirmParameter1 { get; set; }
    public virtual FirmParameter FirmParameter2 { get; set; }
}

4 个答案:

答案 0 :(得分:1)

试着试一试:

var isDbTableQuery = dbContext.FirmFeatures.Where(f => f.FeatureId == featureId && f.IsDbTable);
var parameters = dbContext.FirmParameters.Where(p => isDbTableQuery.Any() ? p.FeatureId == null : p.FeatureId == featureId);
var list = parameters.ToList();

答案 1 :(得分:0)

我现在无法测试它,但如果您唯一的问题是两次往返,您可以使用两个LEFT连接并选择合适的源来完成。

类似的东西:

var query = from feature in dbContext.FirmFeatures

            join parameter0 in dbContext.FirmParameters
              on new { IsDbTable = feature.IsDbTable, FeatureId = feature.FeatureId } equals new { IsDbTable = false, FeatureId = parameter0.FeatureId ?? 0 }
              into left_parameter_0
              from parameter_0 in left_parameter_0.DefaultIfEmpty()

            join parameter1 in dbContext.FirmParameters
              on new { IsDbTable = feature.IsDbTable, FeatureId = (byte?)null } equals new { IsDbTable = true, FeatureId = parameter1.FeatureId }
              into left_parameter_1
              from parameter_1 in left_parameter_1.DefaultIfEmpty()

select new { Feature = feature, Parameter = parameter_0 != null ? parameter_0 : parameter_1 };

var list = query.ToList();

答案 2 :(得分:0)

您可以将条件放在join语句中。我将在查询语法中执行此操作,因为使用连接总是更容易阅读:

var q = from f in dbContext.FirmFeatures
        where f.FeatureId == featureId
        join p in dbContext.FirmParameters on 
            (f.IsDbTable ? null : f.FeatureId) equals p.FeatureId
        select new { p, f };

或者简单地说:

var q2 = from p in dbContext.FirmParameters.Include(p => p.FirmFeature)
         where (p.FirmFeature.FeatureId == featureId && p.FirmFeature.IsDbTable)
            || p.Feature == null
         select p;

您使用IncludeFirmParameters加载FirmFeature次引用(如果有的话)。

答案 3 :(得分:0)

var list = dbContext.FirmParameters
     .Where(p => (p.FirmFeature.FeatureId == featureId && p.FirmFeature.IsDbTable) ? 
            p.FeatureId == null : p.FeatureId == featureId)
     .ToList();

<强>更新

var list = dbContext.FirmParameters
           .Join(dbContext.FirmFeature, p.FeatureId, f.FeatureId, (p, f) => new { Parameter = p, Feature = f})
           .Where(@f => @f.Feature.FeatureId == featureId)
           .Where(@p => (@p.Feature.IsDbTable ? @p.Parameter.FeatureId == null : @p.Parameter.FeatureId == featureId))
           .Select(@x => new { Feature = @x.Feature, Parameter = @x.Parameter })
           .DefaultIfEmpty()
           .ToList();