LINQ: Select where object does not contain items from list

后端 未结 4 1231
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 11:51

I\'m struggling with LINQ syntax here...thought I\'d toss it out here. I cant find exactly what I\'m looking for anywhere else.

OK, say I\'ve got this:



        
相关标签:
4条回答
  • 2020-12-02 12:17

    In general, you're looking for the "Except" extension.

    var rejectStatus = GenerateRejectStatuses();
    var fullList = GenerateFullList();
    var rejectList = fullList.Where(i => rejectStatus.Contains(i.Status));
    var filteredList = fullList.Except(rejectList);
    

    In this example, GenerateRegectStatuses() should be the list of statuses you wish to reject (or in more concrete terms based on your example, a List<int> of IDs)

    0 讨论(0)
  • 2020-12-02 12:20

    Try this simple LINQ:

    //For a file list/array
    var files = Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories);
    
    //simply use Where ! x.Contains
    var notContain = files.Where(x => ! x.Contains(@"\$RECYCLE.BIN\")).ToList();
    
    //Or use Except()
    var containing = files.Where(x => x.Contains(@"\$RECYCLE.BIN\")).ToList();
        notContain = files.Except(containing).ToList();
    
    0 讨论(0)
  • 2020-12-02 12:21

    dump this into a more specific collection of just the ids you don't want

    var notTheseBarIds = filterBars.Select(fb => fb.BarId);
    

    then try this:

    fooSelect = (from f in fooBunch
                 where !notTheseBarIds.Contains(f.BarId)
                 select f).ToList();
    

    or this:

    fooSelect = fooBunch.Where(f => !notTheseBarIds.Contains(f.BarId)).ToList();
    
    0 讨论(0)
  • 2020-12-02 12:35

    I have not tried this, so I am not guarantueeing anything, however

    foreach Bar f in filterBars
    {
         search(f)
    }
    Foo search(Bar b)
    {
        fooSelect = (from f in fooBunch
                     where !(from b in f.BarList select b.BarId).Contains(b.ID)
                     select f).ToList();
    
        return fooSelect;
    }
    
    0 讨论(0)
提交回复
热议问题