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

InvalidCastException when filtering a projected list #4094

Closed
alaatm opened this issue Dec 15, 2015 · 1 comment
Closed

InvalidCastException when filtering a projected list #4094

alaatm opened this issue Dec 15, 2015 · 1 comment
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

@alaatm
Copy link
Contributor

alaatm commented Dec 15, 2015

Using version 7.0.0-rc2-16556 which is the latest on myget as of this writing.

Consider the below 2 entities:

public class Expense
{
    public Guid Id { get; set; }
    public Guid CategoryId { get; set; }
    public Category Category { get; set; }
    public decimal Amount { get; set; }
}

public class Category
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Expense> ExpenseList { get; set; }
}

When entities have a PK field of type Guid such as above, the below code throws InvalidCastException when ToList() runs. The same code executes without any exceptions when the PK field is of type int although the executed result is wrong as it returns empty list where it should return a list with 1 item in it (See linked repro).

var q = from c in db.Categories
        select new
        {
            Id = c.Id,
            Name = c.Name,
            TotalTransactions = c.ExpenseList.Count(),
            TotalAmount = c.ExpenseList.Sum(e => e.Amount)
        };

var filter = q.Where(p => p.TotalTransactions > 2);
var result = filter.ToList(); // Throws InvalidCastException when Id is Guid but no exceptions are thrown when Id is int.

I have included a repro here: http://1drv.ms/1P5iSGA - Filename: EF7Issues.rar
This tests both cases where PK field is int in repro1.cs and when PK field is Guid in repro2.cs.

Note: Both cases throw InvalidCastException in 7.0.0-rc1-final.

@smitpatel
Copy link
Contributor

related to #4908
The incorrect ordering of columns and materializer makes the comparison on fk property during client eval to use the count value instead of actual Id. Therefore for int PK its incorrect result and for Guid its cast exception.

@smitpatel smitpatel modified the milestones: 1.0.0-rc2, 1.0.0 Mar 29, 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
@ajcvickers ajcvickers modified the milestones: 1.0.0-rc2, 1.0.0 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