C# - 如何搜索ObservableCollection?

时间:2014-08-13 13:17:33

标签: c# windows-phone-7

我正在开发Windows Phone 7中的应用程序。我有一个包含可观察集合项目的列表框。现在我想搜索那个列表框项目。我想过滤在文本框中输入的项目。如果我输入'a'表示列表框项应该显示以'a'开头的项目。如果我输入'az'表示它应该显示以'az'开头的项目。

public class SortingExampleViewModel : ReactiveObject
    {
        public string _SearchText = "";
        public string SearchText
        {
            get { return _SearchText; }
            set { this.RaiseAndSetIfChanged(x => x.SearchText, value); }
        }

        public static ObservableCollection<items> _sordtedList;
        public ObservableCollection<items> sordtedList
        {
            get { return _sordtedList; }
            set { this.RaiseAndSetIfChanged(x => x.sordtedList, value); }
        }

        public static ObservableCollection<items> _tempSordtedList;
        public ObservableCollection<items> tempSordtedList
        {
            get { return _tempSordtedList; }
            set { this.RaiseAndSetIfChanged(x => x.tempSordtedList, value); }
        }


        public ReactiveAsyncCommand ExecuteSearch { get; set; }

        public SortingExampleViewModel()
        {
            ObservableCollection<items> myData = new ObservableCollection<items>()
            {
                new items(){firstName = "Vijay Dhas",age=27},
                new items(){firstName = "Ramaraj",age=28},
                new items(){firstName = "Arun",age=29},
                new items(){firstName = "Prabhu",age=30},
                new items(){firstName = "Pranesh",age=31},
                new items(){firstName = "Testing",age=32}
            };


            sordtedList = new ObservableCollection<items>(from i in myData orderby i.firstName select i);

            var canConfirm = this.WhenAny(x => x.SearchText, (search) => SearchMethod(search.Value));
            ExecuteSearch = new ReactiveAsyncCommand(canConfirm, 0);
        }

        public Boolean SearchMethod(String searchValue)
        {
            var col = (ObservableCollection<items>)(sordtedList.Where(p => p.firstName.Contains(searchValue)));
            foreach (items objTest in col)
            {
                tempSordtedList.Add(objTest);
            }
            sordtedList = tempSordtedList;
            return true;
        }
    }

    public class items
    {
        public string firstName { get; set; }
        public int age { get; set; }
    }

但是我在这一行得到错误:

var col = (ObservableCollection<items>)(sordtedList.Where(p => p.firstName.Contains(searchValue)));

显示未知错误。  请帮我在列表框中搜索。

2 个答案:

答案 0 :(得分:1)

我得到了解决方案。

 public SortingExampleViewModel()
        {
            ObservableCollection<items> myData = new ObservableCollection<items>()
            {
                new items(){firstName = "Vijay Dhas",age=27},
                new items(){firstName = "Ramaraj",age=28},
                new items(){firstName = "Arun",age=29},
                new items(){firstName = "Prabhu",age=30},
                new items(){firstName = "Pranesh",age=31},
                new items(){firstName = "Testing",age=32}
            };


            sordtedList = new ObservableCollection<items>(from i in myData orderby i.firstName select i);
            temp = sordtedList;       

            var canConfirm = this.WhenAny(x => x.SearchText, (search) => SearchMethod(search.Value));
            ExecuteSearch = new ReactiveAsyncCommand(canConfirm, 0);
        }

 public Boolean SearchMethod(String searchValue)
        {
            ObservableCollection<items>  tempList = new ObservableCollection<items>();            
            tempList = temp;
            tempSordtedList = new ObservableCollection<items>();
            foreach (items items in tempList)
            {
                if (items.firstName.ToLower().StartsWith(searchValue.ToLower()))
                {
                    tempSordtedList.Add(items);
                }
            }

            sordtedList = tempSordtedList;
            return true;
        }

答案 1 :(得分:0)

您正在向IEnumerable投射ObservableCollection(Linq查询的结果)。

请改用:

var col = sordtedList.Where(p => p.firstName.Contains(searchValue));

由于您只使用变量col来迭代它,因此您无需强制转换它。