2013-06-20 49 views
1

下面的查詢連接上SERVERID兩個表,並返回該服務器ID的和屬於他們的組件如下圖所示如何用SQL語法和<>重寫這個LINQ查詢?

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 

如何檢索以上結果,清除記錄共同組件?這可以通過使用<>不等於來實現。上述代碼如何實現?

+0

所以,你只是想排除的結果'常見component'設置? – Justin

+1

@demo_user檢查[Jon Skeet的回答](http://stackoverflow.com/a/3762875/1671639),你一定會明白。 – Praveen

+0

[使用Linq不等於]的可能重複(http://stackoverflow.com/questions/3669507/using-linq-not-equals) – nawfal

回答

2

只要把約束的組成部分:

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

非常感謝..工作在一秒鐘! –

1

在你select投影,只是過濾出來:

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