Linq Method does not contain a definition for (Contains)

wavemaster 311 Reputation points
2021-03-31T02:33:26.887+00:00

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

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

I 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));

Not sure what to make of this error message:

'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>'

I am out of ideas on how to proceed here.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,138 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,377 questions
0 comments No comments
{count} votes

Accepted answer
  1. Paul Smith 76 Reputation points
    2021-04-01T04:09:24.27+00:00
    var result = services.Where(svc => !addOnOptions.Any(aoo => aoo == svc.svcId));
    

    This will give the answer that your sample data requires.

    Your code with 'contains' implies that you want the inverse of this, remove the not operator

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


1 additional answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,006 Reputation points Microsoft Vendor
    2021-03-31T08:33:22.49+00:00

    Hi @MyLadys-4900,

    I 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));

    From your description, I assume you want to filter the service array based on the addOnOptions value. If that is the case, the LINQ query statement should like this:

    var services = new ServiceGrid [] {   
        new ServiceGrid() { svcId = 5, svcName = "serviceA", svcBillingRate = 10.00M },  
        new ServiceGrid() { svcId = 19, svcName = "serviceB", svcBillingRate = 19.25M },  
        new ServiceGrid() { svcId = 11, svcName = "serviceC", svcBillingRate = 24.00M },  
        new ServiceGrid() { svcId = 17, svcName = "serviceD", svcBillingRate = 4.5M },  
        new ServiceGrid() { svcId = 2, svcName = "serviceE", svcBillingRate = 9.00M }  
    };  
    var addOnOptions = new HashSet<int> { 11, 17 };  
    //Find service from the array which its svcId not in the addOnOptions.  
    var result = services.ToList().Where(c => !addOnOptions.ToList().Contains(c.svcId)).ToList();  
    //Find service from the array which its ID in the options.  
    var result2 = services.ToList().Where(c => addOnOptions.ToList().Contains(c.svcId)).ToList();  
    

    The result like this:

    83251-31.gif

    ------
    If the answer doesn’t solve your issue, please provide more details of error that will help us track down what’s happening.
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    Dillion

    0 comments No comments