使用另一个查询的相同字段从 LINQ 构造对象

发布于 2024-07-26 18:58:32 字数 724 浏览 11 评论 0原文

这是

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ζ澈沫 2024-08-02 18:58:32

来自 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 };

爱冒险 2024-08-02 18:58:32

这满足您的需求吗?

Master = new Master() { Field1 = v.Field1, Field2 = v.Field2, Field3 = v.Field3 }

Does this meet your needs?

Master = new Master() { Field1 = v.Field1, Field2 = v.Field2, Field3 = v.Field3 }
友欢 2024-08-02 18:58:32

对我来说这听起来很奇怪。 使用视图的原因是对模型有不同的“视图”,而不是访问原始模型。

如果您确实需要的话,我建议您继续加入 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.

深爱成瘾 2024-08-02 18:58:32

尝试使用 DataLoadOptions 和 LoadWith,请参阅: http://dotnet.org.za/hiltong/archive/2008/02/12/lazy-loading-in-linq-loadwith-and-associatewith-dataloadoptions- part-1-loadwith.aspx

例如

System.Data.Linq.DataLoadOptions dl = new System.Data.Linq.DataLoadOptions();
dl.LoadWith<Detail>( detail => detail.Master )

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.

System.Data.Linq.DataLoadOptions dl = new System.Data.Linq.DataLoadOptions();
dl.LoadWith<Detail>( detail => detail.Master )
筑梦 2024-08-02 18:58:32

你不能做吗
来自 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
};
?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文