c# - 如何将原始查询字段映射到实体的类字段?
问题描述
我有一个查询:
Select p.Name, p.ProductGroupID as GroupID, g.Name as GroupName
FROM Products as p
LEFT JOIN ProductGroups as g
ON p.ProductGroupID = g.ProductGroupID
我可以使用 dbContext.Database.SqlQuery(query) 来获取 ProductView 的列表,其中 ProductView 是
class ProductView
{
string Name;
GroupView Group;
}
class GroupView
{
int id;
string Name;
}
解决方案
SqlQuery 不支持复杂类型
你应该做的是:
internal class TempResult
{
public string Name { get; set; }
public int? id { get; set; }
public int? AddressId { get; set; }
public string groupName { get; set; }
}
var tempResults = db.Database.SqlQuery<TempResult>(
@"Select p.Name, p.ProductGroupID as GroupID, g.Name as GroupName
FROM Products as p
LEFT JOIN ProductGroups as g
ON p.ProductGroupID = g.ProductGroupID",
objectParameterList.ToArray()).ToList();
querySearchResult = tempResults.Select(t => new SearchResult
{
Name = t.Name,
Group = new GroupView
{
id = t.id,
Name = t.groupName,
}
}
推荐阅读
- android - 如何在 Flutter 发布 apk 中禁用代码混淆?
- db2 - 如何在 DB2 z/OS 中将完整数据库上的授权选择授予用户
- python - ValueError:无法将字符串转换为浮点数:'-
- authentication - Httpie auth 插件发出 POST 请求 FORMAT
- mysql - Mysql日期范围查询慢
- xcode - 以编程方式添加 ODR 资源
- django - Djnago 发布“喜欢”功能问题
- javascript - JSON.parse 在我的字符串上返回 undefined
- javascript - 有什么方法可以复制 div 的呈现 HTML?
- python - 为什么我的 2D FFT 卷积取代了结果?