Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从viewModel和List进行LINQ过滤<&燃气轮机;_Linq_C# 4.0 - Fatal编程技术网

从viewModel和List进行LINQ过滤<&燃气轮机;

从viewModel和List进行LINQ过滤<&燃气轮机;,linq,c#-4.0,Linq,C# 4.0,我需要LINQ语法或方法学方面的帮助,不确定是哪种 这是我的问题:我有一个项目列表(Bill、Bob、Ed),我需要选择并过滤掉用户选择的任何内容。因此,如果viewModel包含“Bob”,那么LINQ语句应该返回“Bill”、“Ed”。诀窍是用户可以选择多个对象,因此viewModel可以包含“Ed”、“Bob”,因此LINQ语句应该只返回“Bill” viewModel是一个IEnumerable,项列表是一个列表。我的出发点很简单: c.Items.select(p=>p.Name

我需要LINQ语法或方法学方面的帮助,不确定是哪种

这是我的问题:我有一个项目列表(Bill、Bob、Ed),我需要选择并过滤掉用户选择的任何内容。因此,如果viewModel包含“Bob”,那么LINQ语句应该返回“Bill”、“Ed”。诀窍是用户可以选择多个对象,因此viewModel可以包含“Ed”、“Bob”,因此LINQ语句应该只返回“Bill”

viewModel是一个IEnumerable,项列表是一个列表。我的出发点很简单:

c.Items.select(p=>p.Name) 
其中c.项目指上述账单、Bob和Ed。现在我只需要过滤掉viewModel选项,我正在努力使用LINQ语法。我已经试过了!=viewModel.selectedNames,它不起作用,一些变体使用.contains,一个变体使用all

var filteredItems = viewModel.selectedNames;
c.Items.Where(p => filteredItems.All(t => !p.Name.Contains(t)));

我现在感觉到搁浅了。

可能是这样的:

var filteredNames = new HashSet<string>(viewModel.SelectedNames);

// nb: this is not strictly the same as your example code,
// but perhaps what you intended    
c.Items.Where(p => !filteredNames.Contains(p.Name));

?@StevenJeuris:
filteredItems
c.Items
的类型不同,这排除了
之外的
public class PeopleViewModel : ViewModelBaseOfYourLiking
{
    public ObservableCollection<Person> AllPeople
    {
        get;
        private set;
    }

    public ObservableCollection<Person> SelectedPeople
    {
        get;
        private set;
    }

    public IEnumerable<Person> ValidPeople
    {
        get { return this.AllPeople.Except(this.SelectedPeople); }
    }

    // ...
public PeopleViewModel(IEnumerable<Person> people)
{
    this.AllPeople = new ObservableCollection<Person>(people);
    this.SelectedPeople = new ObservableCollection<Person>();

    // wire up events to track ValidPeople (optionally do the same to AllPeople)
    this.SelectedPeople.CollectionChanged
        += (sender,e) => { this.RaisePropertyChanged("ValidPeople"); };
}