Is there anyway to remove the three conditionals and use the one the problem is i can have one or more array lengths dependent on how many times the string can be split by a space.
if (array.Length == 1)
{
list = FamilyDataItems.Cast<ListViewItem>()
.Where(x => x.SubItems
.Cast<ListViewItem.ListViewSubItem>()
.Any(y => y.Text.ToLowerInvariant().Contains(array[0])))
.ToArray();
}
else if (array.Length == 2)
{
list = FamilyDataItems.Cast<ListViewItem>()
.Where(x => x.SubItems
.Cast<ListViewItem.ListViewSubItem>()
.Any(y => y.Text.ToLowerInvariant().Contains(array[0])
&& y.Text.ToLowerInvariant().Contains(array[1])))
.ToArray();
}
else if (array.Length > 2)
{
list = FamilyDataItems.Cast<ListViewItem>()
.Where(x => x.SubItems
.Cast<ListViewItem.ListViewSubItem>()
.Any(y => y.Text.ToLowerInvariant().Contains(array[0])
&& y.Text.ToLowerInvariant().Contains(array[1])
&& y.Text.ToLowerInvariant().Contains(array[2])))
.ToArray();
}
According to your comment above, yes the comments suggestions won't get what you need. I think the following will.
If a SubItem
should contain any word from the array:
void Caller()
{
foreach (var item in yourListView.Items.OfType<ListViewItem>())
item.BackColor = SystemColors.Window;
var array = new[] { "door", "double" };
var list = yourListView.Items.Cast<ListViewItem>()
.Where(x => x.SubItems.Cast<ListViewItem.ListViewSubItem>()
.Any(y => array
.Any(z => y.Text.IndexOf(z, StringComparison.OrdinalIgnoreCase) >= 0)));
foreach (var item in list)
item.BackColor = Color.Red;
}
Or if a SubItem
should contain all the words of the array:
void Caller()
{
foreach (var item in yourListView.Items.OfType<ListViewItem>())
item.BackColor = SystemColors.Window;
var array = new[] { "door", "double" };
var list = yourListView.Items.Cast<ListViewItem>()
.Where(x => x.SubItems.Cast<ListViewItem.ListViewSubItem>()
.Any(y => array
.All(z => y.Text.IndexOf(z, StringComparison.OrdinalIgnoreCase) >= 0)));
foreach (var item in list)
item.BackColor = Color.Red;
}