1

以下查询连接 serverID 上的两个表并返回 ServerID 和属于它们的组件,如下所示

LINQ

var header  = from a in this.db.Servers
               where a.ServerID.Contains(match)
               join b in this.db.Components
               on a.ServerID equals b.ServerID into g
               select new
               {
               a.ServerID,                                
               Comp = g.Select(x => x.Name),                              
               };

输出

Server X
common component
component 1x
component 2x
component 3x
Server Y
common component
component 1y
component 2y
component 3y
Server Z
common component
component 1z
component 2z
component 3z

如何检索上述结果,删除记录公共组件?这可以在 SQL 中使用 <> 不等于来实现。上面的代码如何实现?

4

2 回答 2

2

只需对组件进行约束:

select new
{
   a.ServerID,                                
   Comp = g.Where(x => x.Name != "common component").Select(x => x.Name),                              
}
于 2013-06-20T13:48:19.527 回答
1

在您的select投影中,只需将其过滤掉:

Comp = g.Where(x => x.Name != "common component").Select(x => x.Name), 
于 2013-06-20T13:48:02.257 回答