由网友(你瞒我瞒£)分享简介:我要选择一个数据库中的多个列,我没有匹配的实体。所以我的查询看起来是这样的:I have to select multiple columns from a database and I don't have a matching entity.so my query looks like this:var r...
我要选择一个数据库中的多个列,我没有匹配的实体。 所以我的查询看起来是这样的:
I have to select multiple columns from a database and I don't have a matching entity. so my query looks like this:
var result = _dbContext.Database.SqlQuery<List<string>>(
"select ID, NAME, DB_FIELD from eis_hierarchy");
我收到的结果集,每一行包含字符串列表中,但数为0。
I am getting the result set, each row contains list of strings but count is 0.
因此,我怎么选择使用多列 Database.SqlQuery
?
So how do I select multiple columns using Database.SqlQuery
?
推荐答案
您必须捕获结果为一类,与匹配的属性名称,和(至少)有一个参数的构造函数:
You have to capture the results into a class with matching property names, and (at least) a parameterless constructor:
class DbResult
{
public int ID { get; set; }
public string NAME { get; set; }
public string DB_FIELD { get; set; }
}
var result = _dbContext.Database.SqlQuery<DbResult>(
"select ID, NAME, DB_FIELD from eis_hierarchy");
相关推荐
最新文章