locked
Linq - does not contain a definition for 'Contains' RRS feed

  • Question

  • User379720387 posted

    I have an array of ServiceGrid items called services, the relevant properties are shown below:

    svcId / svcName / svcBillingRate
    5 / serviceA / 10.00
    19 / serviceB / 19.25
    11 / serviceC / 24.00
    17 / serviceD / 4.50
    2 / serviceE / 9.00

    also have a hashset<int> addOnOptions that has a number of svcIds, say 11 and 17

    My objective is to end up with:

     5 / serviceA / 10.00 
    19 / serviceB / 19.25 
     2 / serviceE / 9.00
    var theSelectedAddOns = addOnOptions.ToList().Where(x => new[] {services.ToList()}.Contains(x)); 

    This gives the following errors:

    'List<ServiceGrid>[]' does not contain a definition for 'Contains' and the best extension method overload 'MemoryExtensions.Contains<int>(ReadOnlySpan<int>, int)' requires a receiver of type 'ReadOnlySpan<int>'

    In SQL this would be very easy:

    select * from services where svcId in (5, 19, 2)

    Looking for ideas on how to proceed.

    Wednesday, March 31, 2021 2:38 AM

Answers

  • User303363814 posted

    var result = services.Where(svc => !addOnOptions.Any(aoo => aoo == svc..svcId));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 31, 2021 4:56 AM

All replies

  • User303363814 posted

    var result = services.Where(svc => !addOnOptions.Any(aoo => aoo == svc..svcId));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 31, 2021 4:56 AM
  • User1120430333 posted

    Contains() I thought was a string function. So to me,  everything in the array must be converted to string,  and  'x' itself must be x.ToString(). You might have to use a substring() based on the length of the search criteria's length in order to get the hit. 

    Even the link  is talking about a string comparison type.

    MemoryExtensions.Contains Method (System) | Microsoft Docs

    C# | Check if an array contain the elements that match the specified conditions - GeeksforGeeks

    I think you need another approach, becuase Linq is not a  panacea solution.

    Wednesday, March 31, 2021 5:31 AM
  • User379720387 posted

    It is a beauty!

    var result = services.Where(svc => addOnOptions.Any(aoo => aoo == svc..svcId));

    Without the negation.

    Thnx

    @DA924 .... no kidding, I am torn between "unnecessary complication" or maybe a "one of these days I will understand"

    Wednesday, March 31, 2021 5:44 PM