getValue linq错误[英] GetValue LINQ Error

本文是小编为大家收集整理的关于getValue linq错误的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试使用以下代码检索实体的特定属性的值.

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

目前我只是在玩这个,但是在某个时候,我希望能够用任何列名替换" spell_id"文本并动态地获得值.不确定我是否要解决这个问题,但是我会遇到以下错误: -

linq到实体不识别方法'system.Object getValue(system.Object)'方法,并且该方法无法翻译成商店表达式.

我知道我不是以正确的方式做到这一点(而且我对C#MVC/Linq相对较新),但是我花了很多时间修补我迷路的代码...可以有人把我指向正确的方向?

推荐答案

您的当前代码使用反射来获取属性的值,但是,从您可以从您的异常消息中推断出的内容,db是实体框架DbContext.

实体框架根本不支持反射,因为然后通过框架本身将LINQ查询转换为SQL查询.因此,如果您确实需要获得一个属性,则必须更改方法:

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

这将从数据库中获取所有资源,然后在内存中执行Select部分.

如果您只期望从数据库中获得单一实体,那么这是一种更好的方法:

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

其他推荐答案

不要在开玩笑的情况下进行tunge,而是错误

linq到实体不识别方法'system.Object getValue(system.Object)'方法,并且该方法无法翻译成商店表达式.

正是听起来的样子. LINQ无法将GetValue()方法转换为实体框架确实做的任何事情.

有一些方法可以使EF和Linq识别方法,但有点痛苦.最快的解决方案是仅使用循环.

本文地址:https://www.itbaoku.cn/post/1557003.html

问题描述

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

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

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?

推荐答案

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 single 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");

其他推荐答案

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

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

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.