efcore icon indicating copy to clipboard operation
efcore copied to clipboard

NOT IN Doesnt work

Open GVX111 opened this issue 10 years ago • 2 comments

IN RC1-Final doesnt work Contains filter. I have like this query: _tsContext.Hotels.Where(e => hotelCities.Contains(e.CityId)) but it generate this query: SELECT [e].[HotelId], [e].[CategoryId], [e].[CityId], [e].[HotelTitle], [e].[IsRecommended], [e].[Latitude], [e].[LocalityId], [e].[Longitude], [e].[ParentId], [e].[Web] FROM [TS].[Hotels] AS [e]

Where hotelCities variable is IEnumerable

there is not (IN Function) doesn't filtering. @rowanmiller can you help me

GVX111 avatar Feb 22 '16 22:02 GVX111

I am not able to reproduce this issue (using RC1). Below is the code I used to try and reproduce it (and the generated SQL), can you adjust the code listing to demonstrate the issue you are seeing.

using Microsoft.Data.Entity;
using System.Collections.Generic;
using System.Linq;

namespace RC1.Demo
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var context = new MyContext())
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                var hotelCities = new int[] { 1, 2, 3 };

                context.Hotels
                    .Where(e => hotelCities.Contains(e.CityId))
                    .ToList();
            }
        }
    }

    public class MyContext : DbContext
    {
        public DbSet<Hotel> Hotels { get; set; }
        public DbSet<City> Cities { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Sample;Trusted_Connection=True;");
        }
    }

    public class Hotel
    {
        public int HotelId { get; set; }
        public string Name { get; set; }

        public int CityId { get; set; }
        public City City { get; set; }
    }

    public class City
    {
        public int CityId { get; set; }
        public string Name { get; set; }

        public List<Hotel> Hotels { get; set; }
    }
}

Here is the resulting SQL

SELECT [e].[HotelId], [e].[CityId], [e].[Name]
FROM [Hotel] AS [e]
WHERE [e].[CityId] IN (1, 2, 3)

rowanmiller avatar Feb 26 '16 18:02 rowanmiller

I just fixed this issue. problem was City Id in Hotels was short and cities was list of ints. it was problem of cast. int is larger than short. thank you @rowanmiller for help.

GVX111 avatar Feb 26 '16 21:02 GVX111