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

Query: Select with new DTO taking entity in constructor argument throws ArgumentException #8282

Closed
NPadrutt opened this issue Apr 25, 2017 · 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

@NPadrutt
Copy link

I get this error when I Execute the following Linq Statement:

dataContext.Set<AccountEntity>()
     .Where(x => !x.IsExcluded)
     .Select(account => new Account(account))
     .ToListAsync();

This is the whole exception message:

System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'

inner exception:
ArgumentException: The given expression 'new Account([x])' does not contain the searched expression '[x]' in a nested NewExpression with member assignments or a MemberBindingExpression.

As other already pointed out here #4807 the issue does not occur if you add AsNoTracking() to the query.
Also I found out, that the issue only occurs when the Select statement is added. When you have it as follows it works:

var list = dataContext.Set<AccountEntity>()
     .Where(x => !x.IsExcluded)
     .ToListAsync();

list.Select(account => new Account(account))

Account Entity looks as follows:

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        public string Name { get; set; }
        public string Iban { get; set; }
        public double CurrentBalance { get; set; }
        public string Note { get; set; }
        public bool IsOverdrawn { get; set; }
        public bool IsExcluded { get; set; }

        public virtual List<PaymentEntity> ChargedPayments { get; set; }
        public virtual List<PaymentEntity> TargetedPayments { get; set; }

        public virtual List<RecurringPaymentEntity> ChargedRecurringPayments { get; set; }
        public virtual List<RecurringPaymentEntity> TargetedRecurringPayments { get; set; }
@smitpatel
Copy link
Member

This repros in nightly builds too.

@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label May 3, 2017
@smitpatel smitpatel changed the title The given expression does not contain the searched expression '[x]' in a nested NewExpression with member assignments or a MemberBindingExpression. Query: Select with new DTO taking entity in constructor argument throws ArgumentException May 3, 2017
@bricelam bricelam modified the milestones: 2.0.0, 2.0.0-preview2 May 16, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview2, 2.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