Saturday, 15 September 2012

c# - LinqToEntities Generic Classes and Interface -



c# - LinqToEntities Generic Classes and Interface -

public class coreentity { public int id { get; set; } //some additional mutual fields insertedby, insertedat etc } public interface iuniqueobject { guid uid { get; set; } } public class tag : coreentity { public string tagname { get; set; } } public class itemtags : coreentity { public guid taggeditemuid { get; set; } public int tagid { get; set; } } public class repository<t> t : coreentity { public dbset<t> theentityset { get; set; } public dbset<itemtags> itemtagset { get; set; } public iqueryable<t> addtagsfilter(iqueryable<t> query, list<int> tags) { var ts = itemtagset; if (typeof (t) iuniqueobject) { query = query.where(f => ts.any(e => e.taggeditemuid == ((iuniqueobject)f).uid && tags.contains(e.tagid)); } homecoming query; } } //usage public class departman : coreentity, iuniqueobject { public guid uid { get; set; } //some other fields } class test { void xx() { var r = new repository<departman>(); } }

the classes simplified purpose of problem much more shown here. thing linq entities not allow casting ((iuniqueobject)f).uid. how can apply tags filter knowing generic type t implements iuniqueobject interface. tried using genericmethod "t2 : coreentity , iuniqueobject" not cast type t t2, anyways help appreciated.

the statement in linqtoentities converted sql syntax. every instruction must translatable sql - means no casting. still thinking more helpful answer. think you'll need have distinct functions different query types, e.g. add together method like:

public iqueryable<t> addtagsfiltertouniqueobject(iqueryable<t> query, list<int> tags) t : iuniqueobject { var ts = itemtagset; query = query.where(f => ts.any(e => e.taggeditemuid == (f.uid && tags.contains(e.tagid)); homecoming query; }

c# generics linq-to-entities

No comments:

Post a Comment