sebastienros/yessql

Is there a way to reuse join with multiple conditions?

Closed this issue · 1 comments

@sebastienros is there a way to reuse join statement? I hope I am just using the query builder incorrectly.

Here is an example of how I am building my predicts, the problem with the below approach is that every time With<> is called, a new join in added to the sql query. I don't really want that behavior and wondering if there is a work around. So from the code below I call With<DrilldownPartIndex> multiple time, I really want to use the same join statement.

private async Task<IQuery<ContentItem>> BuilQueryAsync(VehicleLookup lookup)
{
    if (lookup == null)
    {
        throw new ArgumentNullException();
    }

    var conditions = new List<Func<IQuery<ContentItem>, IQuery<ContentItem>>>();

    if (lookup.YearIds != null && lookup.YearIds.Any())
    {
        conditions.Add(q => q.With<DrilldownPartIndex>(x => x.YearContentItemId != null && x.YearContentItemId.IsIn(lookup.YearIds)));
    }

    if (lookup.MakeIds != null && lookup.MakeIds.Any())
    {
        conditions.Add(q => q.With<DrilldownPartIndex>(x => x.MakeContentItemId != null && x.MakeContentItemId.IsIn(lookup.MakeIds)));
    }

    if (lookup.ModelIds != null && lookup.ModelIds.Any())
    {
        conditions.Add(q => q.With<DrilldownPartIndex>(x => x.ModelContentItemId != null && x.ModelContentItemId.IsIn(lookup.ModelIds)));
    }

    if (lookup.SeriesIds != null && lookup.SeriesIds.Any())
    {
        conditions.Add(q => q.With<DrilldownPartIndex>(x => x.SeriesContentItemId != null && x.SeriesContentItemId.IsIn(lookup.SeriesIds)));
    }

    if (lookup.StyleIds != null && lookup.StyleIds.Any())
    {
        conditions.Add(q => q.With<DrilldownPartIndex>(x => x.StyleContentItemId != null && x.StyleContentItemId.IsIn(lookup.StyleIds)));
    }

    if (lookup.Status != null )
    {
        conditions.Add(q => q.With<VehicleIndex>(x => x.Status != null && x.Status == lookup.Status);
    }

    return _session.Query<ContentItem>().All(conditions.ToArray());
}
q.With<DrilldownPartIndex>(
q.With<DrilldownPartIndex>(
q.With<DrilldownPartIndex>(
q.With<DrilldownPartIndex>(
q.With<DrilldownPartIndex>(
q.With<DrilldownPartIndex>(

Should work without .All