2017-06-30 20 views
-2

我對IQueryable有疑問。請勿在IQueryable中選擇特定項目

如何刪除/不選擇IQueryable中的項目。

我的代碼返回一個IQueryable與這些實體列表。

{ 
"ResourceId": "FirstName", 
"LanguageId": "ENG", 
"Client": NULL, 
"ResourceText": "first name x" 
} 
{ 
"ResourceId": "FirstName", 
"LanguageId": "ENG", 
"Client": 1, 
"ResourceText": "first name y" 
} 
{ 
"ResourceId": "LastName", 
"LanguageId": "ENG", 
"Client": NULL, 
"ResourceText": "last name" 
} 
{ 
"ResourceId": "BirthDate" 
"LanguageId": "ENG" 
"Client": NULL 
"ResourceText": "date of birth" 
} 

如果存在與特定的客戶端(不爲空),用於同一RESOURCEID我想刪除與客戶端== NULL

對於上述示例的實體的實體的結果應該是

{ 
"ResourceId": "FirstName", 
"LanguageId": "ENG", 
"Client": 1, 
"ResourceText": "first name y" 
} 
{ 
"ResourceId": "LastName", 
"LanguageId": "ENG", 
"Client": NULL, 
"ResourceText": "last name" 
} 
{ 
"ResourceId": "BirthDate" 
"LanguageId": "ENG" 
"Client": NULL 
"ResourceText": "date of birth" 
} 

THX對您有所幫助

+0

這個問題還不夠清楚,你的意思是你在內存列表中有這個,並且你想對它做一個linq查詢嗎? –

回答

1
var result = clients.Where(c => c.Client == null); 

哪裏clientsIQueryable個實體

0

我會做到這一點的方法:

我們有客戶clients的名單。首先,我們要得到所有ResourceID的地方Client不爲空:

var notNullClients = from c in clients where c.Client != null 
        select c.ResourceID; 

然後你只用where條款如下圖所示篩選clints集:

var Clients = from c in clients 
       where ((c.Client == null & !notNullClients.Contains(c.ResourceID)) | 
        (c.Client != null & notNullClients.Contains(c.ResourceID))) 
       select c; 

首先,我們要採取客戶與Client字段等於null只有當沒有這樣的ResourceID,爲此我們有Client非空。然後,我們採取其餘的「非空客戶」。

相關問題