Filter IEnumerable against IEnumerable

前端 未结 4 961
轻奢々
轻奢々 2021-01-20 18:38

I have an object Style with a property StockNumber. I would like to filter a list of all Db.Styles against an IEnumerable

相关标签:
4条回答
  • 2021-01-20 18:47
    public IEnumerable<Style> LoadListOfStyles(List<string> stockNumbers)
    {
        return db.Styles.Where(s => stockNumbers.Contains(s.StockNumber));
    }
    
    0 讨论(0)
  • 2021-01-20 18:59

    Consider a Where clause.

    public IEnumerable<Style> LoadListOfStyles(IEnumerable<string> stockNumbers)
    {
        return Db.Styles.Where(style => !stockNumbers.Contains(style.StockNumber));
    }
    
    0 讨论(0)
  • 2021-01-20 19:06
    return db.Styles.Where(style => stockNumbers.Any(sn => sn == style.StockNumber));
    
    0 讨论(0)
  • 2021-01-20 19:07

    You can think of Intersect as effectively a Join where the keys of each object are always "themselves". Here you want to perform a join where the key isn't always "itself", so you use a Join (or, if you need to remove duplicates, a GroupJoin).

    var query = from style in Db.Styles
        join number in stockNumbers
        on style.StockNumber equals number
        into numbers
        where numbers.Any()
        select style;
    
    0 讨论(0)
提交回复
热议问题