Skip to content

.Contains fails with using a IReadOnlySet #26437

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Grauenwolf opened this issue Oct 22, 2021 · 4 comments · Fixed by #26487
Closed

.Contains fails with using a IReadOnlySet #26437

Grauenwolf opened this issue Oct 22, 2021 · 4 comments · Fixed by #26487
Assignees
Labels
area-query customer-reported good first issue This issue should be relatively straightforward to fix. type-enhancement
Milestone

Comments

@Grauenwolf
Copy link

File a bug

.Contains fails with using a IReadOnlySet.

Include your code

To fix any bug we must first reproduce it. To make this possible, please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing.

It is often impossible for us to reproduce a bug when working with only code snippets since we have to guess at the missing code.

Use triple-tick code fences for any posted code. For example:

IReadOnlySet<int> searchIds = [...] 
var result = persons.Where(p => p.Locations.Any(l => searchIds.Contains(l.Id)));

A work-around is to explicitly cast it to the IEnumerable<int> base interface.

IReadOnlySet<int> searchIds = [...] 
var temp = (IEnumerable<int>)searchIds;
var result = persons.Where(p => p.Locations.Any(l => searchIds.Contains(l.Id)));

Include stack traces

Include the full exception message and stack trace for any exception you encounter.

Use triple-tick fences for stack traces. For example:

    .Where(s => __searchIds_0.Contains(l.Id))' could not be translated. Additional information: Translation of method 'System.Collections.Generic.IReadOnlySet<int>.Contains' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.


Include provider and version information

EF Core version: 5.0.10
Database provider: Microsoft.EntityFrameworkCore.SqlServer 5.0.10
Target framework: .NET 5.0
Operating system: Windows 10

@ajcvickers
Copy link
Contributor

@smitpatel to find dupe.

@smitpatel
Copy link
Contributor

No dupe, need to update following method

public static bool IsContainsMethod(this MethodInfo method)
=> method.Name == nameof(IList.Contains)
&& method.DeclaringType != null
&& method.DeclaringType.GetInterfaces().Append(method.DeclaringType).Any(
t => t == typeof(IList)
|| (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(ICollection<>)));

IReadOnlyCollection doesn't derive from ICollection causing us to fail in this case.

@smitpatel smitpatel added area-query type-enhancement good first issue This issue should be relatively straightforward to fix. labels Oct 26, 2021
@ajcvickers
Copy link
Contributor

@smitpatel Related to #26433 or not?

@smitpatel
Copy link
Contributor

Not related. #26433 is on navigation property which is readonly collection. This one is client side object being readonly collection.

This issue was closed.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
area-query customer-reported good first issue This issue should be relatively straightforward to fix. type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants