使用另一个查询的相同字段从 LINQ 构造对象
这是
SQL
create view v_join
as select m.* , d.oneDetail from master m, detail d
where m.key = d.key
LINQ
var view = from v in dc.v_join select new
{
Master = ???? /// that is an issue, how can I construct the objects using same fields but from another query,
Detail = v.oneDetail
};
foreach (var entry in view)
{
Master mEntry = entry.Master; // then we can use it, there are no problems
}
的示例。这种情况经常遇到,是否有一些棘手的方法可以使用相同的字段但从另一个视图构建对象,并使用。
当我在 C# 端进行此类连接时,性能较低,存在延迟加载问题等。无论如何,LINQ 会发出许多查询来获取每个详细记录,从性能的角度来看,这是不可接受的。
必须有明显的解决方案,我不敢相信没有人遇到同样的问题。 所有明显的解决方案(例如 dc.Translate)的功能几乎相同,但不完全符合我的需要。
帮助表示赞赏。
That's example
SQL
create view v_join
as select m.* , d.oneDetail from master m, detail d
where m.key = d.key
LINQ
var view = from v in dc.v_join select new
{
Master = ???? /// that is an issue, how can I construct the objects using same fields but from another query,
Detail = v.oneDetail
};
foreach (var entry in view)
{
Master mEntry = entry.Master; // then we can use it, there are no problems
}
This situation meets rather often and is there some tricky way to construct the object using same fields but from another view, and uses.
When I make such join at C#-side, I get low performance, issues with lazy loads etc. And anyway LINQ issues many queries to fetch each detail record, is is inacceptible at the performance point of view.
There must be obvious solution, I cannot believe nobody faced same problem.
All obvious solutions like dc.Translate do almost same but not exactly what I need.
Help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
来自 dc.Master 中的 m
将 d 加入 dc.Detail 中 m.Key = d.Key
选择新的{m,d};
from m in dc.Master
join d in dc.Detail on m.Key = d.Key
select new { m, d };
这满足您的需求吗?
Does this meet your needs?
对我来说这听起来很奇怪。 使用视图的原因是对模型有不同的“视图”,而不是访问原始模型。
如果您确实需要的话,我建议您继续加入 LINQ 中的主从表,您可以使用 loadWith 选项。 经过正确调整后,LINQ 可以进行急切加载,从而解决性能问题。
To me this sounds strange. The reason why you use view is to have different "view" on the model, rather than accessing the raw model as well.
I suggest you stay joining the master-detail table in LINQ if you really need so, you can get rid of the lazy loading by using the loadWith Options. While correctly tweaked, LINQ can do the eager loading which solve the performance issue.
尝试使用 DataLoadOptions 和 LoadWith,请参阅: http://dotnet.org.za/hiltong/archive/2008/02/12/lazy-loading-in-linq-loadwith-and-associatewith-dataloadoptions- part-1-loadwith.aspx
例如
Try using DataLoadOptions and LoadWith, see: http://dotnet.org.za/hiltong/archive/2008/02/12/lazy-loading-in-linq-loadwith-and-associatewith-dataloadoptions-part-1-loadwith.aspx
E.g.
你不能做吗
来自 dc.v_join 中的 v
选择新的{
主 = 新 { Column1Name = v.masterColumn1, Column2Name = v.masterColumn2, ... },
详细信息 = v.oneDetail
};
?
Can't you do
from v in dc.v_join
select new {
Master = new { Column1Name = v.masterColumn1, Column2Name = v.masterColumn2, ... },
Detail = v.oneDetail
};
?