LINQ/EF:执行返回实体和计算值的多个联接的概念/机制
我希望有人能帮助我解决如何使用返回实体的 EF 进行多重连接的脑块,其中一个字段是通过查询计算的。我设计了一个(几乎无用的)示例,希望有人能帮助我理解应该如何完成。
我想从 DBContext 返回 ISP 实体列表,其中“TotalUsed”属性由 LINQ 查询填充。我已经成功地完成了连接(微不足道),并且玩过分组和求和,但似乎无法完全正确。
这个设计示例的总体思路是,NIC 连接到一个且仅一个路由器,并且多个路由器分别连接到一个 ISP。
如何编写 LINQ 查询来汇总每个 ISP 所需的带宽以及该 ISP 中的其他属性?
预期输出将是 ISP 列表,如下所示 {{1, "foo", 52}, {2, "bar", 345}, {3, "foobar", 621}},其中第三个属性是与 ISP 传递相关的所有 NIC 上的 BandwidthNeeded 属性的总和。
课程:
public class ISP
{
public int ISPid {get; set;}
public int ISPName {get; set;}
public int TotalUsed {get; set;} // not mapped to DB -> should populate via LINQ
}
public class Router
{
public int RouterId {get; set;}
public string RouterName {get; set;}
public int ISPId {get; set;} // foreign key for ISP
}
public class Nic
{
public int NicId { get; set; }
public string NicLocation { get; set; }
public int BandwidthUsed { get; set; }
public int RouterId {get; set; } // foreign key for router
}
Im hoping someone can help me with a brain block I'm having about how to do multiple joins with EF that returns an entity, where one of the fields is calculated by a query. Ive contrived a (pretty much useless) example in the hopes somebody can help me understand how this should be done.
Id like to return a list of ISPs entities from a DBContext with the "TotalUsed" property filled in by the LINQ query. Ive successfully done the joins (trivial), and have played with grouping and sum, but cant seem to get it quite right.
The general idea to this contrived example is that NICs are connected to one and only one router, and multiple routers are connected to a single ISP each.
How to write a LINQ query that sums the bandwidth needed for each ISP, along with the other properties in that ISP?
Expected output would be a list of ISP that might look like
{{1, "foo", 52}, {2, "bar", 345}, {3, "foobar", 621}}, where the 3rd property is the summation of the BandwidthNeeded property on all NICs transitively related to ISP.
The classes:
public class ISP
{
public int ISPid {get; set;}
public int ISPName {get; set;}
public int TotalUsed {get; set;} // not mapped to DB -> should populate via LINQ
}
public class Router
{
public int RouterId {get; set;}
public string RouterName {get; set;}
public int ISPId {get; set;} // foreign key for ISP
}
public class Nic
{
public int NicId { get; set; }
public string NicLocation { get; set; }
public int BandwidthUsed { get; set; }
public int RouterId {get; set; } // foreign key for router
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果将 TotalUsed 属性移至单独的类型中,则可以执行以下操作(请原谅任何拼写错误):
如果添加导航属性,则可以通过删除 ID 上的链接来使其更具可读性。
If you move the TotalUsed property into a separate type, you could do this (please excuse any typos):
If you add navigation properties, it could be made somewhat more readable by removing the links on ID.
这对你有用吗?这个名字并不漂亮,你可能会在一个查询中得到所有的内容。但很难判断它是否运行得更快。
Does this work for you? The name thing is not pretty and you might get it all into one query. It's hard to tell if it runs faster though.
像这样的事情应该可行:
我会悬停从 ISP 中删除 TotalUsed 属性,并创建一个同时包含 ISP 和 TotalUsed 的包装类。然后您可以删除第二个查询。 (EF 不允许您在实体查询中构造实体)。
Something like this should work:
I would hoverwer remove the TotalUsed propery from ISP and instead create a wrapper class that holds both an ISP and the TotalUsed. Then you could remove the second query. (EF will not let you contruct an entity inside an entity query).