question

wavemaster avatar image
0 Votes"
wavemaster asked wavemaster commented

Linq Method does not contain a definition for (Contains)

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.

dotnet-aspnet-core-generaldotnet-aspnet-core-blazor
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PaulSmith-7226 avatar image
0 Votes"
PaulSmith-7226 answered wavemaster commented
 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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Both answers are accepted, however I prefer the second as it does not have the .ToList() conversions.

Also, from reading up on .Any() vs .Contains(). it appears the former would need fewer clicks as well.

ZhiLv thnx for showing how to make a short demo, that will be helpful for illustrating more complex questions.


0 Votes 0 ·
ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered

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





31.gif (235.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.