49986

GetValue LINQ Error

Question:

I'm trying to retrieve the value for a particular property of an entity into a variable using the following code.

var item = db.Notices .Where(a => a.ID == 0) .Select(x => x .GetType() .GetProperty("Spell_ID") .GetValue(x));

I'm just playing around with this at the moment, but at some point I'd like to be able to replace the 'Spell_ID' text with any column name and get the value dynamically. Not sure if I'm going the right way around this, but I'm getting the following error:-

<em>LINQ to Entities does not recognize the method 'System.Object GetValue(System.Object)' method, and this method cannot be translated into a store expression.</em>

I know I'm not doing this the right way (and I'm relatively new to C# MVC / LINQ), but I've spent so much time tinkering with the code I've lost my way...can somebody point me in the right direction please?

Answer1:

Your current code uses reflection to get the value of a property, but, from what I can infer from your exception message, db is an Entity Framework DbContext.

Entity framework does not support reflection at all, because your LINQ query is then converted into a SQL query by the framework itself. For this reason you have to change your approach if you really need to get a single property:

var items = db.Notices.Where(a => a.ID == 0).ToList(); var itemsProperty = items.Select(x => x.GetType().GetProperty("Spell_ID"));

This will fetch all the resources from the database and then execute the Select part in memory.

If you expect only a <strong>single</strong> entity from your database than this is a better approach:

var entity = db.Notices.SingleOrDefault(a => a.ID == 0); var property = entity.GetType().GetProperty("Spell_ID");

Answer2:

Not to be tounge-in-cheek, but the error

<em>LINQ to Entities does not recognize the method 'System.Object GetValue(System.Object)' method, and this method cannot be translated into a store expression.</em>

is exactly what it sounds like. LINQ is unable to translate the GetValue() method to whatever it is that Entity Framework does exactly.

While there are ways to get EF and LINQ to recognize methods, its kinda a pain. The quickest solution would be to just use a loop.

Recommend

  • ZXING intent request code (identifying my intent)
  • Jhipster: How to create relationships with User entity using supplied tools?
  • JPA/EclipseLink Returning No Results
  • Send multimedia commands
  • Many to Many in Linq using Dapper
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Multiple Left Join LINQ-to-entities
  • Exception creating JSON with LINQ
  • abstracting over a collection
  • Linq Merge lists
  • List images(01.png) and descriptions(01.txt) from directory
  • Invalid object name 'dbo.Item'
  • Force show.bind execution
  • Groovy: Unexpected token “:”
  • Replace value with Factor in r data.table
  • How to use an array of arrays with array_map(…) in PHP?
  • Initializer list vs. initialization method
  • How to access EntityManager inside Entity class in EJB3
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Nant, Vault & Windows Integrated Authentication
  • MySQL WHERE-condition in procedure ignored
  • Trying to switch camera back to front but getting exception
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Web-crawler for facebook in python
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • trying to dynamically update Highchart column chart but series undefined
  • Free memory of cv::Mat loaded using FileStorage API
  • Hits per day in Google Big Query
  • Angular 2 constructor injection vs direct access
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How do I configure my settings file to work with unit tests?
  • How does Linux kernel interrupt the application?
  • Linking SubReports Without LinkChild/LinkMaster
  • Is it possible to post an object from jquery to bottle.py?
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Programmatically clearing map cache
  • java string with new operator and a literal