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

variable 't0' of type ... referenced from scope '', but it is not defined #5107

Closed
lostmsu opened this issue Apr 19, 2016 · 1 comment
Closed
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@lostmsu
Copy link

lostmsu commented Apr 19, 2016

Seems to be similar to #4596
Repro project

var context = new ModelContext(connectionString);
var query = context.Offers.Include(offer => offer.Item).AsNoTracking();
query = query.Where(offer => offer.Price < 100);
for (int i = 0; i < 3; i++) {
    string propName = i.ToString(CultureInfo.InvariantCulture);
    int minimalValue = i*100;
    query = query.Where(offer =>
        offer.Item.Components.Any(prop => prop.Name == propName && prop.Value >= minimalValue));
}

query.ToArray();

Models:

public class Offer
{
    public Guid Id { get; set; }
    public Item Item { get; set; }
    public decimal Price { get; set; }
}

public class Item
{
    public Guid Id { get; set; }
    public Component[] Components { get;set; }
}

public class Component
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public long Value { get; set; }
}

public class ModelContext : DbContext
{
    readonly string connectionString;

    public ModelContext(string connectionString) { this.connectionString = connectionString; }

    public DbSet<Item> Items { get; set; }
    public DbSet<Component> Components { get; set; }
    public DbSet<Offer> Offers { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
        optionsBuilder.UseSqlServer(this.connectionString);
    }
}

I am also not sure, but for the exact same query I see this in warnings:

The LINQ expression 'join Item offer.Item in value(Microsoft.Data.Entity.Query.Internal.EntityQueryable`1[Item]) on Property([offer], "ItemId") equals Convert(Property([offer.Item], "Id"))' could not be translated and will be evaluated locally.

That is bad, as I expected Any to be translated to EXISTS clause.

@maumar
Copy link
Contributor

maumar commented May 16, 2016

This has been fixed in the current bits and the exception is no longer thrown. The reason why you see client evaluation warnings is due to #4588

If navigation between Offer and Item is marked as required, we produce the following query instead:

SELECT [offer].[Id], [offer].[ItemId], [offer].[Price], [offer.Item1].[Id], [offer.Item0].[Id], [offer.Item].[Id], [i].[Id]
                FROM [Offers] AS [offer]
                INNER JOIN [Items] AS [offer.Item] ON [offer].[ItemId] = [offer.Item].[Id]
                INNER JOIN [Items] AS [offer.Item0] ON [offer].[ItemId] = [offer.Item0].[Id]
                INNER JOIN [Items] AS [offer.Item1] ON [offer].[ItemId] = [offer.Item1].[Id]
                INNER JOIN [Items] AS [i] ON [offer].[ItemId] = [i].[Id]
                WHERE ((([offer].[Price] < 100.0) AND EXISTS (
                    SELECT 1
                    FROM [Components] AS [prop]
                    WHERE (([prop].[Name] = @__propName_0) AND ([prop].[Value] >= @__minimalValue_1)) AND ([offer.Item1].[Id] = [prop].[ItemId]))) AND EXISTS (
                    SELECT 1
                    FROM [Components] AS [prop0]
                    WHERE (([prop0].[Name] = @__propName_2) AND ([prop0].[Value] >= @__minimalValue_3)) AND ([offer.Item0].[Id] = [prop0].[ItemId]))) AND EXISTS (
                    SELECT 1
                    FROM [Components] AS [prop1]
                    WHERE (([prop1].[Name] = @__propName_4) AND ([prop1].[Value] >= @__minimalValue_5)) AND ([offer.Item].[Id] = [prop1].[ItemId]))

@maumar maumar closed this as completed May 16, 2016
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

4 participants