简体   繁体   中英

Reverse Engineering a LINQ Statement

I'm trying to reverse engineer a LINQ statement, any help will be greatly appreciated.

bool isAllUnchecked = !lvTech.Items.Cast<ListViewItem>().Any(lvItem => lvItem.Checked);

My progress so far;

bool isAllUnchecked = true;
foreach(ListviewItem item in lvTech.Items)
{
    if(item.checked)
    {
      isAllUnchecked = false;
    }
}

I'm using Resharper and its' convert to LINQ didn't show up so far. What am I missing?

I think it should be:

bool isAllUnchecked = false; // Variable name doesn't fit what you're doing
                             // Don't forget to change.
foreach(ListViewItem item in lvTech.Items)
{
    if(item.checked)
    {
      isAllUnchecked = true;
      break;
    }
}

Any() checks if there's any item that satisfies the condition, if so it returns true .

And you should rename your variable isAnyChecked or you should negate the result of the query.

This would be the right loop (at least it is what the variable name suggests):

bool isAllUnchecked = true;
foreach(ListviewItem item in lvTech.Items)
{
    if(item.checked)
    {
      isAllUnchecked = false;
      break;
    }
}

This is the same with LINQ:

bool isAllUnchecked = lvTech.Items.Cast<ListViewItem>()
                                  .All(lvItem => !lvItem.Checked);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM