gpt4 book ai didi

c# - 有没有办法简化这个 linq

转载 作者:太空狗 更新时间:2023-10-29 18:21:00 26 4
gpt4 key购买 nike

我有 2 个 bool 标志,需要相应地过滤我的集合。必须有一种更清洁的方法来做到这一点。如果有人能指出我正确的方向,我将不胜感激。谢谢。

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;
}
}

最佳答案

由于 LINQ 使用延迟执行,您只需将 Where() 扩展更改为您的对象,并在您进行时重新分配它:

var _filteredEvents = _events; 
if (!queueEmail)
_filteredEvents = _filteredEvents.Where(ev => ev.QueueTypeEnumText != QueueType.Email.ToString());
if (!queueTextMessaging)
_filteredEvents = _filteredEvents.Where(ev => ev.QueueTypeEnumText != QueueType.TextMessage.ToString());

关于c# - 有没有办法简化这个 linq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5302743/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com