我有两个字符串列表
Lista Listb
------------------------
apple mango
mango grapes
grapes watermelon
pineapple orange
banana
我想在我的组合框中显示lista的所有项目,也选择芒果和葡萄两种常见的项目,并在组合框中预先检查它们
combobox items
---------------------
apple
mango - checked
grapes - checked
pineapple
banana
答案 0 :(得分:2)
以下内容应该为您提供一个包含Lista
值的列表,以及是否应在IsChecked
属性中检查列表中的项目的其他信息:
var result = Lista.Select(a => new
{
Value = a,
IsChecked = Listb.Any(b => b == a)
}).ToList();
其余的只是将结果绑定到组合框。这个步骤取决于您的平台(ASP.NET,WPF,Windows窗体等几乎都有组合框),我相信每个都在互联网上有详细记录,所以你应该首先阅读并试用。
答案 1 :(得分:1)
你可以做到
var listA = new[] {"apple", "mango", "grapes", "pineapple", "banana"};
var listB = new[] {"mango", "grapes", "watermelon", "orange"};
var common = listA.Intersect(listB);
获得重叠
答案 2 :(得分:1)
在Linq中尝试左连接
var listA = new[] { "apple", "mango", "grapes", "pineapple", "banana" }.ToList();
var listB = new[] { "mango", "grapes", "watermelon", "orange" }.ToList();
var listCheckboxItem =
(
from a in listA
join b in listB on a equals b into lst
from item in lst.DefaultIfEmpty()
select new
{
Name = a,
IsChecked = !(string.IsNullOrEmpty(item))
}
).ToList();
答案 3 :(得分:0)
Select
可以获取布尔值列表,显示两个列表中的元素。
List<string> Lista = new List<string>(new string[] { "apple", "mango", "grapes", "pineapple", "banana" });
List<string> Listb = new List<string>(new string[] {"mango","grapes","watermelon","orange"});
var inBoth = Lista.Select(x => Listb.Contains(x));
// False, True, True, False, False