不要在IQueryable

时间:2017-06-30 06:03:08

标签: c#

我有一个关于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求助

2 个答案:

答案 0 :(得分:1)

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

其中clientsIQueryable个实体

答案 1 :(得分: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,我们)不为空。然后我们采取其余的“非空客户”。