c# - Generic method to return common field from different tables -


i have around 15 tables working with, , working on creating audit reports these tables. of these tables have userid column gets updated last user's id made changes. create repository method allow me pass in table entity use method , return list of userids use on audit page, instead of allowing user type whatever want in userid search field audit, want supply dropdown userids in table pass method.

i thinking of this:

var ids = _repo.getuserids(//table entity here, unsure how declare in signature); 

you need make generic method, type parameter represents type entity , access corresponding dbset<t> data collection.

public int getuserid<tentity>() {    int userid;    using (var cts = new mydbcontext())    {       userid = ctx.dbset<tentity>.firstordefault().select(e => e.userid);    }    return userid; } 

to able include projection lambda e => e.userid need make classes implement interface this:

public interface iuserid {   public int userid { get; set; } } 

and generic class must have corresponding type constraint:

public repoclass<t>    t:iuserid {     // define generic method here } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -