当检查可以是字符串数组中的一个或多个项目时,如何将列表视图项目添加到数组?
How to add listviewitems to an array when the check can be one or more items in a string array?
有没有删除三个条件并使用一个的问题是我可以有一个或多个数组长度取决于字符串可以被 space.[=11= 分割多少次]
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();
}
你可以试试这样的 -
list = FamilyDataItems.Cast<ListViewItem>()
.Where(
(array.Length == 1 && x => x.SubItems
.Cast<ListViewItem.ListViewSubItem>()
.Any(y => y.Text.ToLowerInvariant().Contains(array[0])))
|| (array.Length == 2 && x => x.SubItems
.Cast<ListViewItem.ListViewSubItem>()
.Any(y => y.Text.ToLowerInvariant().Contains(array[0])
&& y.Text.ToLowerInvariant().Contains(array[1])))
|| (array.Length > 2 && 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();
HTH,我没有遗漏任何括号
根据您上面的评论,是的,评论建议不会得到您需要的。我觉得下面会。
如果 SubItem
应包含数组中的任何单词:
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;
}
或者如果 SubItem
应该包含数组的所有单词:
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;
}
有没有删除三个条件并使用一个的问题是我可以有一个或多个数组长度取决于字符串可以被 space.[=11= 分割多少次]
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();
}
你可以试试这样的 -
list = FamilyDataItems.Cast<ListViewItem>()
.Where(
(array.Length == 1 && x => x.SubItems
.Cast<ListViewItem.ListViewSubItem>()
.Any(y => y.Text.ToLowerInvariant().Contains(array[0])))
|| (array.Length == 2 && x => x.SubItems
.Cast<ListViewItem.ListViewSubItem>()
.Any(y => y.Text.ToLowerInvariant().Contains(array[0])
&& y.Text.ToLowerInvariant().Contains(array[1])))
|| (array.Length > 2 && 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();
HTH,我没有遗漏任何括号
根据您上面的评论,是的,评论建议不会得到您需要的。我觉得下面会。
如果 SubItem
应包含数组中的任何单词:
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;
}
或者如果 SubItem
应该包含数组的所有单词:
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;
}