我有关于高效实现的问题。可以说我有两个数组:

One array is all possible items in a house: Table, Chair, TV, Fireplace, Bed

The other is an array of items in a particular house: Table, TV, Bed

我也有两个列表框:

1. listbox for items in the house - the "HAS" list box
2. listbox items not in the house - the "NEEDS" list box

我需要已经列出的项目在房子中的“有”列表框中,以及未在房子中的“需要”列表框中的项目。这在我看来,嵌套“对于每一个”循环将是一个开端,以解决这个问题,但我不完全知道需要嵌套这种情况下。什么是要完成这样的任务最有效的方法是什么?

有帮助吗?

解决方案

var allItems = (new [] {"Table", "Chair", "TV", "Fireplace", "Bed"});
var hasItems = (new [] {"Table", "Chair"});

var hasList = hasItems.ToList();
var needsList = allItems.Except(hasItems).ToList();

其他提示

var allList = (new [] {"Table", "Chair", "TV", "Fireplace", "Bed"}).ToList();
var hasList = (new [] {"Table", "Chair"}).ToList();

var hasSet = new HashSet<string>(hasList);
var needsList = allList.Where(i => !hasList.Contains(i)).ToList();

这是〜最快溶液(至少,在大O表示法)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top