Skip to content

Instantly share code, notes, and snippets.

@gandjustas
Created October 24, 2016 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gandjustas/e65c8602b59c86966616fa29a69fe9a6 to your computer and use it in GitHub Desktop.
Save gandjustas/e65c8602b59c86966616fa29a69fe9a6 to your computer and use it in GitHub Desktop.
Interfaces + IQueryable = Love
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