c# - Using reflection to address a Linqed property -
I am trying to write a common method that will load a specific type of record with a specific ID. Here's a way that works:
Public intangible class LinqedTable & lt; T & gt; Where T: LinqableTable {Public Static T Received (Long ID) {DataContext Reference = LinqUtils.GetDataContext & lt; T & gt; (); Var q = From obj in context. GetTable & lt; T & gt; () Obj.id == ID select obj; Return q.Single & lt; T & gt; (); }} Public Intangible Class LinqableTable {public abstract long id {get; Set; }} You can There are some limitations, though. First of all, it forces all of my tables to I want more flexibility than that, Naturally, I tried to reflection, and came out with the following: LinqUtils.GetDataContext & lt; T & gt; () can ignore calls; This is a utility function that I have to deal with the fact that I have many data references in my program. The issue is that now I declare any of my classes as sub sections of LinqableTable I can easily record that code for LinqedTable & lt; MyType & gt; / Code>. long , named id for the type of identity identification class. Secondly, because I am using an abstract method, I must change the inheritance property of every ID field in my system to go to the O / R Designer and in "Override".
Public Essentials LinqedTable & lt; T & gt; Where T: LinqableTable {Public Static T Received (Long ID) {DataContext Reference = LinqUtils.GetDataContext & lt; T & gt; (); Var q = From obj in context. GetTable & lt; T & gt; () Where obj.IDValue == ID select obj; Return q.Single & lt; T & gt; (); }} Public Intangible Class LinqableTable {Internal long IDView {Receives {Return (long) IDProperty.GetValue (this, blank); } Set {IDProperty.SetValue (this, value, zero); }} Internal PropertyInfo IDProperty {get.GetType (return this). GetProperty (IDPropertyName); }} Internal protected virtual string IDPropertyName {get {return "ID"; Theoretically, it allows me to override the ID column name, . long should be corrected with any integral data type, and I 'overrides my code ID to all code No need to define as
But
Link does not like it to call Okay, today I came to know that Linq does some magic at the back end; It does not instantize q.Single & lt; T & gt; (); I get a runtime error: Members 'EISS.Utils.LinqableTable.IDValue' has no support for translation into SQL. obj and just read IDValue property
But what?
But what?
In Linux, Linux tries to translate linq-query into SQL, but in DB it is not known how to translate your property in the name of the column
A good explanation can be found here:
But how to solve it, one more thing I am inheriting the use of the Amarok from this thread:
Or you can see the dynamic query of Scott Guthrie Can abuse are:
Comments
Post a Comment