c# - Generic DBContext for Where Query -
so trying make generic function query, not using repository possible this?
public ienumerable<t> something<t>(int authorid) t : class { return vmsb.set<t>().where(c => c.authorid== authorid); }
now can't because dont know c.authorid is
create interface ihaveauthor
, specify on partial classes property:
public interface ihaveauthor { int authorid { get; set; } } //note interface implemented in auto-generated part. //or if it's code first, specify directly on classes. public partial class book : ihaveauthor { } public partial class article : ihaveauthor { }
then point interface under generic type where
constraint:
public ienumerable<t> getauthorpublicationsof<t>(int authorid) t : class, ihaveauthor { return vmsb.set<t>().where(c => c.authorid == authorid); }
and usage:
var authorbooks = query.getauthorpublicationsof<book>(authorid); var authorarticles = query.getauthorpublicationsof<article>(authorid);
Comments
Post a Comment