Linq查询过滤Observable集合中的字符串列表

时间:2013-03-18 11:22:20

标签: .net list observablecollection

从Observable集合中我想过滤名称为aaa,bbb,ccc,ddd,eee的项目。怎么做?

public class Item
{
public string ItemName {get; set;}
public int ItemNumber {get; set;}
}

public ObservableCollection<Item> _items;

public List<string> searchItems = new List<string>(5);
searchItems.Add("aaa");
searchItems.Add("bbb");
searchItems.Add("ccc");
searchItems.Add("ddd");
searchItems.Add("eee");

我在linq中尝试了一下..但我不知道如何处理列表..

var filteredItems = from item in _items
                    where _items.Any(x => x.ItemName == "aaa")
                    select x;

1 个答案:

答案 0 :(得分:5)

你可以使用Contains;

var filteredItems = from item in _items
                    where _items.Any(x => searchItems.Contains(x.ItemName))
                    /*select x;*/
                    select item;