如何从 LINQ-TO-SQL 中的 ExecuteQuery() 映射额外字段但保留 LINQ 实体
假设我有一个位置表和一个相关的团队表 我需要通过 ExecuteQuery() 调用获取职位实体的列表,以及相关表中的团队名称。如:(
var list = dc.ExecuteQuery<T>("SELECT Position.*, Team.Name AS TeamName " +
"FROM Position LEFT JOIN Team ON Position.TeamID=Team.TeamID");
我需要使用 ExecuteQuery(),因为实际查询非常复杂。)
是的,我可以创建一个新的平面类,其中包含 Position 类中的所有字段加上 teamname 字段,但是我需要结果集是实际的 LINQ 实体,而不仅仅是 POCO,因为我将迭代这些记录并更新一些字段。
第一个想法,创建一个包含位置和新字段的新类,
public class PositionExtended
{
public Position position {get; set;}
public string teamname {get; set;}
}
ExecuteQuery<PositionExtended>("SELECT Position.*, Team.Name AS TeamName " +
"FROM Position LEFT JOIN Team ON Position.TeamID=Team.TeamID");
这可以正确映射团队名称,但不能正确映射位置对象。
第二个想法,继承Position类:
public class PositionExtended : Position
{
public string teamname {get; set;}
}
ExecuteQuery<PositionExtended>("SELECT Position.*, Team.Name AS TeamName " +
"FROM Position LEFT JOIN Team ON Position.TeamID=Team.TeamID");
这会返回一个错误“该字段(Position类的第一个字段)不是PositionExtend类型映射的一部分。是继承根之上的成员吗?”等级制度?”
最后一个想法,使用分部类:
public partial class Position
{
[Column(Name="TeamName")]
public string TeamName {get; set;}
}
ExecuteQuery<Position>("SELECT Position.*, Team.Name AS TeamName " +
"FROM Position LEFT JOIN Team ON Position.TeamID=Team.TeamID");
这实际上适用于这个特定的 SQL 查询,但它更改了 Position 类,并且返回 Positions 的所有其他 LINQ 调用都会失败,因为 teamname 字段实际上并不是 Position 的一部分表,因此不会在查询中返回。
有没有人有解决这些想法的方法,或者更好的想法?
Lets say I have a Position table and a related Team table
I need to get a list of Position entities, plus the Team name from a related table, out of an ExecuteQuery() call. As in:
var list = dc.ExecuteQuery<T>("SELECT Position.*, Team.Name AS TeamName " +
"FROM Position LEFT JOIN Team ON Position.TeamID=Team.TeamID");
(I need to use ExecuteQuery(), because the actual query is very complex.)
Yes, I could create a new flat class with all of the fields from the Position class plus the teamname field, but I need the result set to be actual LINQ entities, not just POCOs, because I will iterate over those records and update some fields.
First idea, Create a new class that contains the Position and the new field
public class PositionExtended
{
public Position position {get; set;}
public string teamname {get; set;}
}
ExecuteQuery<PositionExtended>("SELECT Position.*, Team.Name AS TeamName " +
"FROM Position LEFT JOIN Team ON Position.TeamID=Team.TeamID");
This correctly maps the teamname, but not the position object.
Second Idea, inherit from the Position class:
public class PositionExtended : Position
{
public string teamname {get; set;}
}
ExecuteQuery<PositionExtended>("SELECT Position.*, Team.Name AS TeamName " +
"FROM Position LEFT JOIN Team ON Position.TeamID=Team.TeamID");
This returns an error "the field (first field of the Position class) is not part of the mapping for type PositionExtended. Is the member above the root of an inheritance hierarchy?"
Last idea, Use a partial class:
public partial class Position
{
[Column(Name="TeamName")]
public string TeamName {get; set;}
}
ExecuteQuery<Position>("SELECT Position.*, Team.Name AS TeamName " +
"FROM Position LEFT JOIN Team ON Position.TeamID=Team.TeamID");
This actually works for this specific SQL query, but it changes the Position class, and all other LINQ calls returning Positions fail, because teamname field is not really part of the Position table, thus not returned in the query.
Does anyone have a workaround for any of these ideas, or a better idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更好的想法:
在这些情况下使用 Dapper :)
另一个想法:
添加伪属性。
IOW,将
TeamName
属性添加到Position
类中。如果为null
,则表示未使用它。也不要用 LINQ2SQL 属性来装饰它。还有一个想法:
如果您不关心改变对象,只需创建一个视图并将其放入设计器中即可。如果需要,添加一些额外的属性以解析为真实实体。
Better idea:
Use Dapper for these cases :)
Another idea:
Add a pseudo property.
IOW, add the
TeamName
property to thePosition
class. If it isnull
, it is not being used. Also do not decorate it with LINQ2SQL attributes.Yet another idea:
If you dont care about mutating the objects, just create a view and drop that into the designer. Add some extra properties to resolve to real entities, if needed.
我还没有尝试过,但我面临着非常相似的挑战。我正在考虑使用 Linq.Table 对象的 Attach 方法来手动设置根据 POCO 列表中返回的数据创建的真实实体实例。
I have not tried this yet, but I am facing a very similar challenge. I am considering using the Attach method of the Linq.Table object to manually set up true entity instances created from data returned in a list of POCOs.