-
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
Query: Contains in AndAlso translates to non-boolean value #4860
Comments
I attempted to reproduce the issue using the latest packages @yyjdelete can you confirm if this is still failing for you? If so, could you provide additional information about how your model is created and the specific data that causes the exception to be thrown? Here is the code that I used to attempt to reproduce the issue: class Program
{
static void Main(string[] args)
{
using (var ctx = new ConfusingContext())
{
ctx.Database.EnsureDeleted();
ctx.Database.EnsureCreated();
ctx.Add(new UserDeviceMap
{
UserId = 1025,
Status = 0,
DeviceId = 1
});
ctx.Add(new UserDeviceMap
{
UserId = 1025,
Status = 0,
DeviceId = null
});
ctx.Add(new DeviceMsgUp()
{
DeviceId = 1
});
ctx.SaveChanges();
}
using (var ctx = new ConfusingContext())
{
var count =
ctx.DeviceMsgUps
.GroupJoin(ctx.UserDeviceMaps.Where(udp => udp.UserId == 1025 && udp.Status == 0 && udp.DeviceId != null), dmu => dmu.DeviceId, udp => udp.DeviceId, (key, vals) => new { key, vals })
.Where(pair => pair.vals.Any())
.Select(pair => pair.key)
.Select(key => new { key.Format })
.Count();
}
}
public class ConfusingContext : DbContext
{
public DbSet<DeviceMsgUp> DeviceMsgUps { get; set; }
public DbSet<UserDeviceMap> UserDeviceMaps { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=ConfusingErrorTest;Integrated Security=True");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<UserDeviceMap>().HasKey(udm => udm.UdMapId);
modelBuilder.Entity<DeviceMsgUp>().HasKey(dmu => dmu.MessageId);
}
}
[GeneratedCode("EF.Reverse.POCO.Generator", "2.18.1.0")]
public class DeviceMsgUp
{
public int MessageId { get; set; } // Message_ID (Primary key)
public int DeviceId { get; set; } // Device_ID
public byte Format { get; set; } // Format
}
[GeneratedCode("EF.Reverse.POCO.Generator", "2.18.1.0")]
public partial class UserDeviceMap
{
public int UdMapId { get; set; } // UDMap_ID (Primary key)
public int UserId { get; set; } // User_ID
public int? DeviceId { get; set; } // Device_ID
public byte Status { get; set; } // Status
}
} |
@mikary The same with using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace SF.School.Dal73
{
class Program
{
static void Main(string[] args)
{
PrepareData();
Test4860();
Test4858();
Console.ReadKey(true);
}
private static void Test4858()
{
using (var ctx = GetDbContext())
{
var query = ctx.DeviceMsgUps
.GroupJoin(ctx.UserDeviceMaps.Where(udp => udp.UserId == 1025 && udp.DeviceId != null), dmu => dmu.DeviceId, udp => udp.DeviceId, (key, vals) => new { key, vals })
.Where(pair => pair.vals.Any());
int count1 = query.Count();//Expect 1, Actual 1
int count2 = query.Select(pair => pair.key).Count();//Expect 1, Actual 3
int count3 = query.Select(pair => pair.key).Select(key => new { key.Action }).Count();//Expect 1, Actual 2
Console.WriteLine($"Result of Test4858 is {count1}/{count2}/{count3}, 1/1/1 is expected.");
}
}
private static void Test4860()
{
using (var ctx = GetDbContext())
{
try
{
var count = ctx.DeviceMsgUps
.Where(dm => dm.Action == 27
&& ctx.UserDeviceMaps
.Where(udp => udp.DeviceId != null).Select(udp => udp.DeviceId).Contains(dm.DeviceId))
.Count();
Console.WriteLine($"Result of Test4860 is {count}");
}
catch (Exception e)
{
Console.Error.WriteLine("Test4860 -> Boom!");
Console.Error.WriteLine(e);
}
}
}
private static void PrepareData()
{
using (var ctx = GetDbContext())
{
ctx.Database.EnsureDeleted();
ctx.Database.EnsureCreated();
ctx.UserDeviceMaps.Add(new UserDeviceMap
{
UserId = 1025,
DeviceId = 1
});
ctx.DeviceMsgUps.Add(new DeviceMsgUp
{
DeviceId = 1,
Action = 27,
});
ctx.DeviceMsgUps.Add(new DeviceMsgUp
{
DeviceId = 1,
Action = 27,
});
ctx.DeviceMsgUps.Add(new DeviceMsgUp
{
DeviceId = 2,
Action = 27,
});
ctx.SaveChanges();
}
}
private static MyDbContext GetDbContext()
{
return new MyDbContext();
}
}
public class MyDbContext : DbContext
{
public DbSet<DeviceMsgUp> DeviceMsgUps { get; set; }
public DbSet<UserDeviceMap> UserDeviceMaps { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=ConfusingErrorTest;Integrated Security=True");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<UserDeviceMap>()
.HasKey(udm => udm.UdMapId);
modelBuilder.Entity<DeviceMsgUp>()
.HasKey(dmu => dmu.MessageId);
}
}
public class DeviceMsgUp
{
public int MessageId { get; set; } //(Primary key)
public int DeviceId { get; set; }
public byte Action { get; set; }
}
public class UserDeviceMap
{
public int UdMapId { get; set; } //(Primary key)
public int UserId { get; set; }
public int? DeviceId { get; set; }
}
} packages.config <?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Ix-Async" version="1.2.5" targetFramework="net461" />
<package id="Microsoft.AspNetCore.Hosting.Abstractions" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.AspNetCore.Hosting.Server.Abstractions" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.AspNetCore.Http.Abstractions" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.AspNetCore.Http.Features" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.EntityFrameworkCore" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.EntityFrameworkCore.Commands" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.EntityFrameworkCore.Relational" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.EntityFrameworkCore.Relational.Design" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.EntityFrameworkCore.SqlServer" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.EntityFrameworkCore.SqlServer.Design" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.Extensions.Caching.Abstractions" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.Extensions.Caching.Memory" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.Extensions.Configuration.Abstractions" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.Extensions.DependencyInjection" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.Extensions.FileProviders.Abstractions" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.Extensions.Logging" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.Extensions.Logging.Abstractions" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.Extensions.Options" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.Extensions.PlatformAbstractions" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Microsoft.Extensions.Primitives" version="1.0.0-rc3-20498" targetFramework="net461" />
<package id="Remotion.Linq" version="2.0.2" targetFramework="net461" />
<package id="System.Collections" version="4.0.11-rc2-23931" targetFramework="net461" />
<package id="System.Collections.Concurrent" version="4.0.12-rc2-23931" targetFramework="net461" />
<package id="System.ComponentModel" version="4.0.1-rc2-23931" targetFramework="net461" />
<package id="System.Diagnostics.Debug" version="4.0.11-rc2-23931" targetFramework="net461" />
<package id="System.Diagnostics.DiagnosticSource" version="4.0.0-rc2-23931" targetFramework="net461" />
<package id="System.Globalization" version="4.0.11-rc2-23931" targetFramework="net461" />
<package id="System.IO" version="4.1.0-rc2-23931" targetFramework="net461" />
<package id="System.Linq" version="4.1.0-rc2-23931" targetFramework="net461" />
<package id="System.Linq.Expressions" version="4.0.11-rc2-23931" targetFramework="net461" />
<package id="System.Reflection" version="4.1.0-rc2-23931" targetFramework="net461" />
<package id="System.Resources.ResourceManager" version="4.0.1-rc2-23931" targetFramework="net461" />
<package id="System.Runtime" version="4.1.0-rc2-23931" targetFramework="net461" />
<package id="System.Runtime.Extensions" version="4.1.0-rc2-23931" targetFramework="net461" />
<package id="System.Runtime.InteropServices" version="4.1.0-rc2-23931" targetFramework="net461" />
<package id="System.Text.Encodings.Web" version="4.0.0-rc2-23931" targetFramework="net461" />
<package id="System.Threading" version="4.0.11-rc2-23931" targetFramework="net461" />
<package id="System.Threading.Tasks" version="4.0.11-rc2-23931" targetFramework="net461" />
</packages> |
There appears to be a bug in the translation of the var count = ctx.DeviceMsgUps
.Where(dm => dm.Action == 27
&& ctx.UserDeviceMaps
.Where(udp => udp.DeviceId != null).Select(udp => udp.DeviceId).Contains(dm.DeviceId))
.Count(); When calling |
clearing milestone for triage |
Closing this issue as it appears to be a duplicate of #3705, Exception thrown for Where with Contains. |
Database:
MSSQL
EntityFramework:
Microsoft.EntityFrameworkCore:1.0.0-rc2-20261(aspnetcidev)
Query
Model
Error
Contains Works(Replace
.Select(udp => udp.DeviceId)
with.Select(udp => udp.DeviceId.Value)
If remove
dm => dm.Action == 27 &&
, it will failed with another Exception, which is much more easy to recognise.The text was updated successfully, but these errors were encountered: