Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Query: Support IReadOnlyCollection<T>.Contains #19320

Closed
wdnijdam opened this issue Dec 14, 2019 · 1 comment
Closed

Query: Support IReadOnlyCollection<T>.Contains #19320

wdnijdam opened this issue Dec 14, 2019 · 1 comment

Comments

@wdnijdam
Copy link

wdnijdam commented Dec 14, 2019

IReadOnlyCollection<>.Contains() cannot be translated to SQL by EF core 3.1
EF core 2.2 supports it

Related issue of ICollection<>: #17599

It seems that a static field fails while a local variable succeeds.

Steps to reproduce

Create DbContext using EF core 3.1
Declare static field "MyCollection" of type IReadOnlyCollection<> and assign values.
Use the field in linq query .Where(x => mycollection.Contains(x.Prop))

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace ConsoleApp1
{
    static class Program
    { 
        private static readonly IReadOnlyCollection<int> MyCollection = new List<int> {5, 6, 7}.AsReadOnly();

        public static void Main()
        {
            using var context = new MyContext();
            context.Database.EnsureCreated();
            var x = context.Examples
                .Where(e => MyCollection.Contains(e.MyProp))
                .ToList();
            Console.Write(x.Count);
        }

        private class MyContext : DbContext
        {
            public DbSet<ExampleTable> Examples { get; set; }

            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseSqlServer(@"Server=(LocalDb)\MSSQLLocalDB;Database=EFCoreCollectionCrash;Trusted_Connection=True;MultipleActiveResultSets=true");
            }
        }

        private class ExampleTable
        {
            public int ExampleTableId { get; set; }
            public int MyProp { get; set; }
        }
        
    }
}
The LINQ expression 'DbSet<ExampleTable>
    .Where(e => (IReadOnlyCollection<int>)ReadOnlyCollection<int> { 5, 6, 7, }
        .Contains(e.MyProp))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

EF Core version: 3.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1
Operating system: Windows x64

Remark this works (as local variable):

public static void Main()
        {
            var myCollection = new List<int> {5, 6, 7}.AsReadOnly();

            using var context = new MyContext();
            context.Database.EnsureCreated();
            var x = context.Examples
                .Where(e => myCollection.Contains(e.MyProp))
                .ToList();
            Console.Write(x.Count);
        }

It also works if the type of the static field is changed to List

@smitpatel
Copy link
Contributor

Duplicate of #18658

@smitpatel smitpatel marked this as a duplicate of #18658 Dec 14, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants