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 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>.

There are some limitations, though. First of all, it forces all of my tables to 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".

I want more flexibility than that, Naturally, I tried to reflection, and came out with the following:

  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 q.Single & lt; T & gt; (); I get a runtime error:

Members 'EISS.Utils.LinqableTable.IDValue' has no support for translation into SQL.

Okay, today I came to know that Linq does some magic at the back end; It does not instantize 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

itemprop = "text">

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

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -