-
-
Save gandjustas/e65c8602b59c86966616fa29a69fe9a6 to your computer and use it in GitHub Desktop.
Interfaces + IQueryable = Love
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
public interface IWithRating | |
{ | |
int Rating { get; set; } | |
} | |
public class Product:IWithRating | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public int Rating { get; set; } | |
} | |
public class StoreContext : DbContext | |
{ | |
public DbSet<Product> Products { get; set; } | |
} | |
public static class RatingExtensions | |
{ | |
public static IQueryable<T> NiceRating<T>(this IQueryable<T> q) where T : class, IWithRating | |
{ | |
return q.Where(x => x.Rating > 50); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
using (var ctx = new StoreContext()) | |
{ | |
ctx.Database.Log = Console.WriteLine; | |
var q = from p in ctx.Products.NiceRating() | |
where p.Name.StartsWith("a") | |
select new { p.Id, p.Name }; | |
q.ToArray(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment