有没有办法简化这个 linq
我有 2 个布尔标志,需要相应地过滤我的集合。必须有一种更清洁的方法来做到这一点。如果有人能指出我正确的方向,我将不胜感激。谢谢。
var _filteredEvents = from ev in _events
select ev;
if (!queueEmail) // don't queue email
{
if (!queueTextMessaging) // don't queue textmessaging
{
_filteredEvents = from ev in _events
where ev.QueueTypeEnumText != QueueType.TextMessage.ToString()
&& ev.QueueTypeEnumText !=QueueType.Email.ToString()
select ev;
}
else
{
_filteredEvents = from ev in _events
where ev.QueueTypeEnumText != QueueType.Email.ToString()
select ev;
}
}
else //queue email
{
if (!queueTextMessaging) //don't queue textmessaging
{
_filteredEvents = from ev in _events
where ev.QueueTypeEnumText != QueueType.TextMessage.ToString()
select ev;
}
}
I have 2 bool flags and need to filter my collection accordingly. There has to be an cleaner way to do this. If anyone can point me in the right direction I'd appreciate it. Thanks.
var _filteredEvents = from ev in _events
select ev;
if (!queueEmail) // don't queue email
{
if (!queueTextMessaging) // don't queue textmessaging
{
_filteredEvents = from ev in _events
where ev.QueueTypeEnumText != QueueType.TextMessage.ToString()
&& ev.QueueTypeEnumText !=QueueType.Email.ToString()
select ev;
}
else
{
_filteredEvents = from ev in _events
where ev.QueueTypeEnumText != QueueType.Email.ToString()
select ev;
}
}
else //queue email
{
if (!queueTextMessaging) //don't queue textmessaging
{
_filteredEvents = from ev in _events
where ev.QueueTypeEnumText != QueueType.TextMessage.ToString()
select ev;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于 LINQ 使用延迟执行,因此您只需更改对象的
Where()
扩展名,然后随时重新分配它:Since LINQ uses deferred execution, you can just change the
Where()
extensions onto your object, and reassign it as you go:您可以创建一个 where 子句:
You can make one where clause:
我只是创建一个方法来为您测试条件
var _filteredEvents = 来自 _events 中的 ev
其中测试(queueEmail,queueTextMessaging,ev)
选择电动车;
...
bool Test(bool foo, bool bar, Event ev) {...}
I would simply create a Method testing the condition for you
var _filteredEvents = from ev in _events
where Test(queueEmail, queueTextMessaging, ev)
select ev;
...
bool Test(bool foo, bool bar, Event ev) {...}