Last active
January 31, 2018 14:39
-
-
Save dandohotaru/218a3bfd588ce0faaeddc17e13a0d07e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var ctx = new DemoContext(); | |
var inner1 = ctx.Filter<Bar>(value: "Const1"); | |
var inner2 = ctx.Filter<Bar>(value: "Const2"); | |
var inner3 = ctx.Filter<Bar>(value: "Const3"); | |
var query = from foo in ctx.Foos | |
select new | |
{ | |
P1 = inner1.FirstOrDefault(p => p.Y == foo.X1), | |
P2 = inner2.FirstOrDefault(p => p.Y == foo.X2), | |
P3 = inner3.FirstOrDefault(p => p.Y == foo.X3), | |
}; | |
query.Dump(); | |
} | |
public static class Extensions | |
{ | |
public static IQueryable<T> Filter<T>(this DemoContext instance, Expression<Func<T, bool>> predicate = null, string value = null) | |
where T : class, IMarker | |
{ | |
return instance | |
.Set<T>() | |
.Where(p => p.SomeProp == value) | |
.Where(predicate); | |
} | |
} | |
public class Foo : IEntity | |
{ | |
public string X1 { get; set; } | |
public string X2 { get; set; } | |
public string X3 { get; set; } | |
} | |
public class Bar : IMarker | |
{ | |
public string SomeProp { get; set; } | |
public string Y { get; set; } | |
} | |
public interface IEntity | |
{ | |
} | |
public interface IMarker : IEntity | |
{ | |
string SomeProp { get; set; } | |
} | |
public class DemoContext : DbContext | |
{ | |
public IDbSet<Foo> Foos { get; set; } | |
public IDbSet<Bar> Bars { get; set; } | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var ctx = new DemoContext(); | |
var query = from foo in ctx.Foos | |
select new | |
{ | |
P1 = ctx.Bars.FirstOrDefault(b => b.Y == foo.X1 && b.SomeProp == "Const1"), | |
P2 = ctx.Find<Bar>(p => p.Y == foo.X2 && p.SomeProp == "Const2" ), | |
P3 = ctx.Find<Bar>(p => p.Y == foo.X3, "Const3"), | |
}; | |
query.Dump(); | |
} | |
public static class Extensions | |
{ | |
public static T Find<T>(this DemoContext instance, Expression<Func<T, bool>> predicate) | |
where T : class, IEntity | |
{ | |
var query = instance | |
.Set<T>() | |
.Where(predicate) | |
.FirstOrDefault(); | |
return query; | |
} | |
public static T Find<T>(this DemoContext instance, Expression<Func<T, bool>> predicate, string value) | |
where T : class, IMarker | |
{ | |
var query = instance | |
.Set<T>() | |
.Where(p => p.SomeProp == value) | |
.Where(predicate) | |
.FirstOrDefault(); | |
return query; | |
} | |
} | |
public class Foo : IEntity | |
{ | |
public string X1 { get; set; } | |
public string X2 { get; set; } | |
public string X3 { get; set; } | |
} | |
public class Bar : IMarker | |
{ | |
public string SomeProp { get; set; } | |
public string Y { get; set; } | |
} | |
public interface IEntity | |
{ | |
} | |
public interface IMarker : IEntity | |
{ | |
string SomeProp { get; set; } | |
} | |
public class DemoContext : DbContext | |
{ | |
public IDbSet<Foo> Foos { get; set; } | |
public IDbSet<Bar> Bars { get; set; } | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment