如何仅返回条件上的特定对象列表基础
我想为返回选定的对象列表添加条件。如果情况满足条件,则如果该项目的类别是此示例,则不会返回该项目,类别为3。
这是查询:
var objectQuery = query
.ToList()
.Select(a => new
{
a.Id,
ObjectDetails = (new ObjectClass[] { })
.Concat(...)
.Concat(...)
.Distinct()
.Select(f => new
{
f.ObjectDetails.Id,
f.ObjectDetails.Name,
f.ObjectDetails.DisplayName,
category = (int)f.ObjectDetails.Category,
f.ObjectDetails.IsArchived,
})
});
return objectQuery.ToList().FirstOrDefault();
我想添加这样的条件使用类别3并返回所有项目(如果没有),但是我不知道如何正确实施它:
if(condition a)
return objectQuery.Select(...Where(category != 3)).ToList().FirstOrDefault();
else
return objectQuery.ToList().FirstOrDefault();
这是回报输出:
{
"objectQuery": {
"id": 1198,
"ObjectDetails": [
{
"id": 1,
"name": "name one",
"displayName": "NAME 1",
"Category": 1,
},
{
"id": 2,
"name": "name two",
"displayName": "NAME 2",
"Category": 1,
},
{
"id": 3,
"name": "name three",
"displayName": "NAME 3",
"Category": 2,
},
{
"id": 4,
"name": "name four",
"displayName": "NAME 4",
"Category": 2,
},
{
"id": 5,
"name": "name five",
"displayName": "NAME 5",
"Category": 3,
},
{
"id": 6,
"name": "name six",
"displayName": "NAME 6",
"Category": 1,
},
{
"id": 7,
"name": "name seven",
"displayName": "NAME 7",
"Category": 1,
}
]
}
}
I want to add a condition to for returning selected object list. If the condition is met, it will not return the an item if its category is for this example, category is 3.
This is the query:
var objectQuery = query
.ToList()
.Select(a => new
{
a.Id,
ObjectDetails = (new ObjectClass[] { })
.Concat(...)
.Concat(...)
.Distinct()
.Select(f => new
{
f.ObjectDetails.Id,
f.ObjectDetails.Name,
f.ObjectDetails.DisplayName,
category = (int)f.ObjectDetails.Category,
f.ObjectDetails.IsArchived,
})
});
return objectQuery.ToList().FirstOrDefault();
i want to add a condition like this where if it met the condition, it will remove all items with category 3 and returns all the item if not, but i dont know how to properly implement it:
if(condition a)
return objectQuery.Select(...Where(category != 3)).ToList().FirstOrDefault();
else
return objectQuery.ToList().FirstOrDefault();
this is the return output:
{
"objectQuery": {
"id": 1198,
"ObjectDetails": [
{
"id": 1,
"name": "name one",
"displayName": "NAME 1",
"Category": 1,
},
{
"id": 2,
"name": "name two",
"displayName": "NAME 2",
"Category": 1,
},
{
"id": 3,
"name": "name three",
"displayName": "NAME 3",
"Category": 2,
},
{
"id": 4,
"name": "name four",
"displayName": "NAME 4",
"Category": 2,
},
{
"id": 5,
"name": "name five",
"displayName": "NAME 5",
"Category": 3,
},
{
"id": 6,
"name": "name six",
"displayName": "NAME 6",
"Category": 1,
},
{
"id": 7,
"name": "name seven",
"displayName": "NAME 7",
"Category": 1,
}
]
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确地得到您的问题,那么您将需要类似的问题:
If I get your question properly then you'll need something like this: