Unique list of items using LINQ

后端 未结 6 1408
星月不相逢
星月不相逢 2021-02-01 05:39

I\'ve been using LINQ for a while now, but seem to be stuck on something with regards to Unique items, I have the folling list:

List stock = new Lis         


        
6条回答
  •  抹茶落季
    2021-02-01 06:04

    I probably don't understand the question, but this sounds to me like a pretty simple Linq-query:

    List stock = new List();
    ... populate your list as per your example
    
    List kitchenAppliances =
        (from obj in stock
         where obj.Type == "Kitchen Appliance"
         select obj).ToList();
    

    or if you prefer the extension-method syntax:

    List kitchenAppliances =
        stock.Where(obj => obj.Type == "Kitchen Appliance").ToList();
    

    What I don't understand is your usage of distinct and unique in this context. The objects are already unique, and it seems to me that you want a basic query "give me all kitchen appliances".

提交回复
热议问题