Closed
Description
1. Description
When UseParameterizedNamesInDynamicQuery = true
in config (for best perfs), can not compare enum type with String
.
2. Project fiddle
https://dotnetfiddle.net/ZKJC6i
3. Source
// @nuget: System.Linq.Dynamic.Core
// @nuget: Z.EntityFramework.Classic
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Linq.Dynamic.Core;
public class Program
{
public static void Main()
{
GenerateData();
var context = new EntityContext();
ParsingConfig.Default.UseParameterizedNamesInDynamicQuery = false;
var query1 = context.Customers
.Where("Gender = @0", "Female");
query1.Dump();
ParsingConfig.Default.UseParameterizedNamesInDynamicQuery = true;
try
{
var query2 = context.Customers
.Where("Gender = @0", "Female");
query2.Dump();
}
catch (Exception ex)
{
ex.Message.Dump();
}
}
public static void GenerateData()
{
var list = new List<Customer>();
list.Add(new Customer()
{
Name = "Terri Lee Duffy",
CompanyName = "ABC",
City = "Paris",
Phone = "333-444444",
Location = new Location() { Name = "test" },
LastContact = DateTimeOffset.Parse("2022-11-14"),
Gender = Gender.Male
});
list.Add(new Customer()
{
Name = "Garry Moore",
CompanyName = "ABC",
City = "Paris",
Phone = "54654-444444",
Location = new Location() { Name = "ohter test", UpdateAt = DateTimeOffset.Parse("2022-11-16") },
LastContact = DateTimeOffset.Parse("2022-11-16"),
Gender = Gender.Female
});
using (var context = new EntityContext())
{
context.Customers.AddRange(list);
context.BulkSaveChanges();
}
}
public class EntityContext : DbContext
{
public EntityContext() : base(FiddleHelper.GetConnectionStringSqlServer())
{
}
public DbSet<Customer> Customers { get; set; }
public DbSet<Location> Locations { get; set; }
}
public class Customer
{
public int CustomerID { get; set; }
public string Name { get; set; }
public string CompanyName { get; set; }
public string City { get; set; }
public string Phone { get; set; }
public Location Location { get; set; }
public DateTimeOffset? LastContact { get; set; }
public Gender Gender { get; set; }
}
public class Location
{
public int LocationID { get; set; }
public string Name { get; set; }
public DateTimeOffset UpdateAt { get; set; }
}
public enum Gender
{
Male = 0,
Female = 1,
Other = 2
}
}
4. Console result
Dumping object(System.Data.Entity.Infrastructure.DbQuery`1[Customer])
[
{
City : Paris
CompanyName : ABC
CustomerID : 2
Gender : Female
LastContact : 11/16/2022 00:00:00 +00:00
Location : null
Name : Garry Moore
Phone : 54654-444444
}
]
Dumping object(String)
Operator '=' incompatible with operand types 'Gender' and 'String'
5. Related issue
(Sorry, me again...)