Filter Out List with another list using LINQ

后端 未结 1 409
抹茶落季
抹茶落季 2021-01-23 22:15

Hi I\'m having difficulty finding an answer to my question here, so I figured I\'d just ask. I have to lists of classes, ServiceItem, and ServiceDetailsClass. I want to filter o

相关标签:
1条回答
  • 2021-01-23 22:44

    You're making a new object and then checking the list to see if that exact object/instance is in it (i.e. because it's an object, it's comparing the reference).

    Instead, you need to look for overlapping IDs.

    Something like this should work:

    List<ServiceItem> serviceItems;
    List<ServiceItemDetails> serviceItemDetails;
    
    var result = serviceItemDetails.Where(sid => serviceItems.Any(si => si.ID == sid.ID))
    

    In English: "The collection of ServiceItemDetails where the list of service items has an item with the same ID"

    0 讨论(0)
提交回复
热议问题