Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/LinqTests/Acceptance/query_with_inheritance.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using JasperFx;
using Marten;
using Marten.Linq;
using Marten.Testing.Harness;
using Shouldly;
using Weasel.Core;
Expand All @@ -26,14 +28,19 @@ public class Smurf: ISmurf

public interface IPapaSmurf: ISmurf
{
bool IsVillageLeader { get; set; }
}

public class PapaSmurf: Smurf, IPapaSmurf
{
public bool IsVillageLeader { get; set; }

public bool IsPapa { get; set; } = true;
}

public class PapySmurf: Smurf, IPapaSmurf
{
public bool IsVillageLeader { get; set; }
}

public class BrainySmurf: PapaSmurf
Expand Down Expand Up @@ -240,5 +247,36 @@ public async Task get_all_subclasses_of_an_interface()
theSession.Query<IPapaSmurf>().Count().ShouldBe(3);
}

[Fact]
public async Task search_on_property_of_subclass()
{
var smurf = new Smurf {Ability = "Follow the herd"};
var papa = new PapaSmurf {Ability = "Lead", IsVillageLeader = true };
var papy = new PapySmurf {Ability = "Lead"};
var brainy = new BrainySmurf {Ability = "Invent"};
theSession.Store(smurf, papa, brainy, papy);

await theSession.SaveChangesAsync();

(await theSession.Query<Smurf>().WhereSub<PapaSmurf>(x => x.IsVillageLeader).CountAsync()).ShouldBe(1);
}

[Fact]
public async Task search_on_property_of_subclass_and_parent()
{
var smurf = new Smurf {Ability = "Follow the herd"};
var papa = new PapaSmurf {Ability = "Lead" };
var papy = new PapySmurf {Ability = "Lead"};
var brainy = new BrainySmurf {Ability = "Invent"};
theSession.Store(smurf, papa, brainy, papy);

await theSession.SaveChangesAsync();

(await theSession.Query<Smurf>()
.WhereSub<PapaSmurf>(x => x.IsPapa)
.Where(x => x.Ability == "Invent")
.CountAsync()).ShouldBe(1);
}

#endregion
}
2 changes: 2 additions & 0 deletions src/Marten/Linq/IMartenQueryable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,6 @@ IMartenQueryableIncludeBuilder<T, TKey, TInclude> Include<TKey, TInclude>(
/// <returns></returns>
IMartenQueryableIncludeBuilder<T, TKey, TInclude> Include<TKey, TInclude>(
IDictionary<TKey, List<TInclude>> dictionary) where TInclude : notnull where TKey : notnull;

IMartenQueryable<T> WhereSub<TSub>(Expression<Func<TSub, bool>> predicate) where TSub : T;
}
6 changes: 6 additions & 0 deletions src/Marten/Linq/MartenLinqQueryable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading;
using System.Threading.Tasks;
using JasperFx.Core;
using JasperFx.Core.Reflection;
using Marten.Internal.Sessions;
using Marten.Linq.Includes;
using Marten.Linq.Parsing;
Expand Down Expand Up @@ -125,6 +126,11 @@ public IMartenQueryableIncludeBuilder<T, TKey, TInclude> Include<TKey, TInclude>
return new MartenQueryableIncludeBuilder<T, TKey, TInclude>(this, dictionary);
}

public IMartenQueryable<T> WhereSub<TSub>(Expression<Func<TSub, bool>> predicate) where TSub : T =>
(IMartenQueryable<T>)this.Where(
Expression.Lambda<Func<T, bool>>(predicate.Body, Expression.Parameter(typeof(T), "x"))
);

public IEnumerator<T> GetEnumerator()
{
return Provider.Execute<IEnumerable<T>>(Expression).GetEnumerator();
Expand Down
Loading