有没有办法简化这个 linq

发布于 2024-10-21 18:48:00 字数 1016 浏览 6 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

宫墨修音 2024-10-28 18:48:00

由于 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());

Since LINQ uses deferred execution, you can just change the Where() extensions onto your object, and reassign it as you go:

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());
雅心素梦 2024-10-28 18:48:00

您可以创建一个 where 子句:

where 
     (queueTextMessaging || ev.QueueTypeEnumText != QueueType.TextMessage.ToString())
   &&(queueEmail || ev.QueueTypeEnumText !=QueueType.Email.ToString())

You can make one where clause:

where 
     (queueTextMessaging || ev.QueueTypeEnumText != QueueType.TextMessage.ToString())
   &&(queueEmail || ev.QueueTypeEnumText !=QueueType.Email.ToString())
羁绊已千年 2024-10-28 18:48:00
var _filteredEvents =
  from ev in _events where (
    queueTextMessaging || ev.QueueTypeEnumText != QueueType.TextMessage.ToString() &&
    queueEmail || ev.QueueTypeEnumText !=QueueType.Email.ToString())
  select ev;
var _filteredEvents =
  from ev in _events where (
    queueTextMessaging || ev.QueueTypeEnumText != QueueType.TextMessage.ToString() &&
    queueEmail || ev.QueueTypeEnumText !=QueueType.Email.ToString())
  select ev;
梦里人 2024-10-28 18:48:00

我只是创建一个方法来为您测试条件


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) {...}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文