[英]Entity framework: Retrieving data with column name and value
If I use: 如果我使用:
private List<string> GetDataFrom()
{
var result = new List<string>();
using (var context = new mainEntities())
{
var matches = context.data.Where(s => s.Width == 500).ToList();
result.AddRange(matches.Select(t => t.Key));
}
return result;
}
It is giving me perfect results, but I want to use a method where I can use column name and value, like this: 它给了我完美的结果,但是我想使用一种可以使用列名和值的方法,如下所示:
private List<string> GetDataFrom(string columnName, int valToMatch)
{
var result = new List<string>();
using (var context = new mainEntities())
{
var propertyInfo = typeof(data).GetProperty(columnName).Name;
var matches = context.data
.Where(p => p.propertyInfo == valToMatch);
result.AddRange(matches.Select(t => t.Key));
}
return result;
}
This Method obviously doesn't work, so how can I do the same? 这种方法显然行不通,那么我该怎么做? I am using SqlLite, so some answers given do not apply.
我正在使用SqlLite,因此给出的某些答案不适用。 The whole problem is using propertyInfo the wrong way.
整个问题是错误地使用propertyInfo。
I tried various different approaches but no success. 我尝试了各种不同的方法,但没有成功。
This question is not a duplicate, because the suggested questions and their answers do not help much. 这个问题不是重复的,因为建议的问题及其答案没有太大帮助。 I like this question to be reopened.
我希望这个问题可以重开。 I have found an answer myself I like to share.
我想分享自己的答案。
ToString() method can not be translated into relevant SQL. ToString()方法无法转换为相关的SQL。
Try to use SqlFunctions.StringConvert(valToMatch) instead of valToMatch.ToString() 尝试使用SqlFunctions.StringConvert(valToMatch)而不是valToMatch.ToString()
.ToString()
Doesn't works inside the Linq Query. 在Linq查询中不起作用。
Inststed of .ToString() Function Use the following 安装.ToString()函数使用以下代码
SqlFunctions.StringConvert()
SEE THE FOLLOWING ANSWER https://stackoverflow.com/a/3292773/3736442 请参阅以下答复https://stackoverflow.com/a/3292773/3736442
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.