-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
IEnumerable and ISet in queries with Contains #19636
Comments
@IgorMenshikov As a workaround, try making the field not Note for team: this looks like a duplicate of something I thought we fixed, but I can't find the duplicate and this still fails on master. public class Operation
{
public int Id { get; set; }
public OperationType Type { get; set; }
public DateTime PlanDate { get; set; }
public DateTime? DeleteDate { get; set; }
public int PeriodID { get; set; }
}
public class Item
{
public Operation Operation { get; set; }
}
public class Parameters
{
public int PeriodID { get; set; }
}
public enum OperationType
{
One,
Two
}
public class BloggingContext : DbContext
{
private readonly ILoggerFactory Logger
= LoggerFactory.Create(c => c.AddConsole());//.SetMinimumLevel(LogLevel.Debug));
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseLoggerFactory(Logger)
.EnableSensitiveDataLogging()
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Operation>();
}
}
public class Program
{
private readonly static IEnumerable<OperationType> typeList = new HashSet<OperationType>
{
OperationType.One,
OperationType.Two
};
public static async Task Main()
{
using (var context = new BloggingContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
}
using (var context = new BloggingContext())
{
var parameters = new Parameters();
var list = (from i in context.Set<Operation>()
where typeList.Contains(i.Type) &&
i.DeleteDate == null &&
i.PeriodID == parameters.PeriodID
orderby i.PlanDate descending
select new Item()
{
Operation = i
}).ToList();
}
}
}
|
I try to avoid accidental changes of variables. That is why I declare them "readonly" with minimal interface if possible. One day I have create an issue #17877, so HashSet.Contains can be used for queries. Comments there tell it was implemented for generic ICollection.Contains. Maybe they are relative but does not look like if removal of "readonly" helps. |
Duplicate of #18658 |
I have a query that filters by a column by a set of values. If I use HashSet variable with set of values, everything works. If I pass IEnumerable or ISet variable of instance of HashSet, I get an exception "could not be translated"
Steps to reproduce
The query I use requests "operation" objects by some filters and create a list of custom class instances using received data:
It works well but if I change typeList to
I get an exception:
Further technical details
EF Core version: 3.1.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET Core 3.1.1
Operating system: Windows
IDE: Visual Studio 2019 16.4 latest
The text was updated successfully, but these errors were encountered: