【问题标题】:How to rewrite this LINQ query with SQL syntax and <>?如何用 SQL 语法和 <> 重写这个 LINQ 查询?
【发布时间】:2013-06-17 09:42:42
【问题描述】:

下面的查询在 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 中使用 不等于来实现。上面的代码如何实现?

【问题讨论】:

标签: c# linq


【解决方案1】:

只需对组件进行约束:

select new
{
   a.ServerID,                                
   Comp = g.Where(x => x.Name != "common component").Select(x => x.Name),                              
}

【讨论】:

    【解决方案2】:

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

    Comp = g.Where(x => x.Name != "common component").Select(x => x.Name), 
    

    【讨论】:

      猜你喜欢
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多