LINQ Null Join with Pivot Table
LINQ Null Join with Pivot Table
我正在尝试获取可能属于或不属于 1 个或多个组的服务器列表以在网格中显示。
示例
ServerID IP GroupID
1 192.168.1.44 1
1 192.168.1.44 10
2 192.168.1.45 1
3 192.168.1.46 2
4 192.168.1.47 null
5 192.168.1.48 null
如果我在 GroupServer Table 中没有记录。 (因为没有组或组存在但没有分配)我希望得到这样的东西:
ServerID IP GroupID
1 192.168.1.44 null
2 192.168.1.45 null
3 192.168.1.46 null
4 192.168.1.47 null
5 192.168.1.48 null
因为是多对多关系。我有
- 组Table
- 服务器Table
- 组服务器Table
我找不到 LINQ Pivot Table 示例。
所以我试着自己建一个。
var query = (from sg in context.ServerGroups
join servers in context.Servers on sg.ServerID equals servers.ID
join groups in context.Groups on sg.GroupID equals groups.ID
into serverxgroup
from gAddrBilling in serverxgroup.DefaultIfEmpty()
select new
{
ServerID = sg.ServerID,
ServerIP = server.IP,
GroupID = sg.GroupID
});
上面的查询没有检索到任何东西
我不明白 "from gAddrBilling" 是干什么用的。因为我修改了一个片段,所以我试图让它工作。所以我想知道是否有人已经遇到过这样的问题,并给我一些提示、片段或建议,说明我缺少什么。
谢谢。
首先,这不是数据透视查询,而是通过显式联结 table.
对多对多关系的常规查询
其次,看起来您正在使用 Entity Framework,在这种情况下,您最好定义和使用 导航属性 而不是手动 join
。
第三,也是最重要的,查询的结构是错误的。如果您想 获取可能属于或不属于 1 个或多个组 的服务器列表,那么您应该从 Servers
开始查询(table您希望始终包含哪些记录,而不是 link table 中缺少某些 ServerID
的记录),然后将 left outer joins 用于其他 tables这个:
var query =
from s in servers in context.Servers
join sg in context.ServerGroups on s.ID equals sg.ServerID
into s_sg from sg in s_sg.DefaultIfEmpty() // make the above LEFT OUTER JOIN
// You can remove the next two lines if all you need is the GroupId
// and keep them if you need some other Group field in the select
join g in context.Groups on sg.GroupID equals g.ID
into sg_g from g in sg_g.DefaultIfEmpty() // make the above LEFT OUTER JOIN
select new
{
ServerID = s.ID,
ServerIP = s.IP, // or sg.IP?
GroupID = (int?)sg.GroupID
};
我正在尝试获取可能属于或不属于 1 个或多个组的服务器列表以在网格中显示。
示例
ServerID IP GroupID
1 192.168.1.44 1
1 192.168.1.44 10
2 192.168.1.45 1
3 192.168.1.46 2
4 192.168.1.47 null
5 192.168.1.48 null
如果我在 GroupServer Table 中没有记录。 (因为没有组或组存在但没有分配)我希望得到这样的东西:
ServerID IP GroupID
1 192.168.1.44 null
2 192.168.1.45 null
3 192.168.1.46 null
4 192.168.1.47 null
5 192.168.1.48 null
因为是多对多关系。我有
- 组Table
- 服务器Table
- 组服务器Table
我找不到 LINQ Pivot Table 示例。 所以我试着自己建一个。
var query = (from sg in context.ServerGroups
join servers in context.Servers on sg.ServerID equals servers.ID
join groups in context.Groups on sg.GroupID equals groups.ID
into serverxgroup
from gAddrBilling in serverxgroup.DefaultIfEmpty()
select new
{
ServerID = sg.ServerID,
ServerIP = server.IP,
GroupID = sg.GroupID
});
上面的查询没有检索到任何东西 我不明白 "from gAddrBilling" 是干什么用的。因为我修改了一个片段,所以我试图让它工作。所以我想知道是否有人已经遇到过这样的问题,并给我一些提示、片段或建议,说明我缺少什么。
谢谢。
首先,这不是数据透视查询,而是通过显式联结 table.
对多对多关系的常规查询其次,看起来您正在使用 Entity Framework,在这种情况下,您最好定义和使用 导航属性 而不是手动 join
。
第三,也是最重要的,查询的结构是错误的。如果您想 获取可能属于或不属于 1 个或多个组 的服务器列表,那么您应该从 Servers
开始查询(table您希望始终包含哪些记录,而不是 link table 中缺少某些 ServerID
的记录),然后将 left outer joins 用于其他 tables这个:
var query =
from s in servers in context.Servers
join sg in context.ServerGroups on s.ID equals sg.ServerID
into s_sg from sg in s_sg.DefaultIfEmpty() // make the above LEFT OUTER JOIN
// You can remove the next two lines if all you need is the GroupId
// and keep them if you need some other Group field in the select
join g in context.Groups on sg.GroupID equals g.ID
into sg_g from g in sg_g.DefaultIfEmpty() // make the above LEFT OUTER JOIN
select new
{
ServerID = s.ID,
ServerIP = s.IP, // or sg.IP?
GroupID = (int?)sg.GroupID
};