I'm using Entityframework-core 2.1.4, want to run following query and trying to get value from navigation property based on some condition.
var openInvoiceData = _Context.Invoice
.Where(i => i.InvoiceDate.Date == model.CurrentDate.Date)
.Select(i => new Contracts.OpenInvoiceData() {
CustomerId = i.BillToId != null && i.BillTo.CustomerBillTo.Where(x => x.CustomerType == Enum.EnumCustomerType.BillTo).FirstOrDefault() != null
? i.BillTo.CustomerBillTo.Where(x => x.CustomerType == Enum.EnumCustomerType.BillTo).FirstOrDefault().Customer.CustomerId
: i.Customer.CustomerId
}).ToList();
When its true part executed, its returning following exception.
System.ArgumentException: Argument types do not match
at System.Linq.Expressions.Expression.Condition(Expression test, Expression ifTrue, Expression ifFalse, Type type)
at System.Linq.Expressions.ConditionalExpression.Update(Expression test, Expression ifTrue, Expression ifFalse)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.VisitConditional(ConditionalExpression expression)
at System.Linq.Expressions.ConditionalExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Remotion.Linq.Parsing.ThrowingExpressionVisitor.Visit(Expression expression)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.Visit(Expression expression)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.VisitUnary(UnaryExpression expression)
at System.Linq.Expressions.UnaryExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Remotion.Linq.Parsing.ThrowingExpressionVisitor.Visit(Expression expression)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.Visit(Expression expression)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalProjectionExpressionVisitor.Visit(Expression expression)
at System.Linq.Expressions.ExpressionVisitor.VisitMemberAssignment(MemberAssignment node)
at System.Linq.Expressions.ExpressionVisitor.VisitMemberBinding(MemberBinding node)
at System.Linq.Expressions.ExpressionVisitor.Visit[T](ReadOnlyCollection`1 nodes, Func`2 elementVisitor)
at System.Linq.Expressions.ExpressionVisitor.VisitMemberInit(MemberInitExpression node)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalProjectionExpressionVisitor.VisitMemberInit(MemberInitExpression memberInitExpression)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalProjectionExpressionVisitor.Visit(Expression expression)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitSelectClause(SelectClause selectClause, QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitSelectClause(SelectClause selectClause, QueryModel queryModel)
at Remotion.Linq.Clauses.SelectClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel)
at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](QueryModel queryModel)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, IQueryModelGenerator queryModelGenerator, IDatabase database, IDiagnosticsLogger`1 logger, Type contextType)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass13_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at Remotion.Linq.QueryableBase`1.GetEnumerator()
at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at TintPro.Repository.InvoiceRepository.GetOpenInvoices(GetOpenInvoice model) in C:\\Devendra Work\\TintPro\\Source BitBucket\\TintProAPI\\Repository\\Repository\\InvoiceRepository.cs:line 163
at TintPro.Services.InvoiceService.GetOpenInvoices(GetOpenInvoice model) in C:\\Devendra Work\\TintPro\\Source BitBucket\\TintProAPI\\Services\\Service\\InvoiceService.cs:line 486
at TintProAPI.Controllers.InvoiceController.GetOpenInvoice(GetOpenInvoice model) in C:\\Devendra Work\\TintPro\\Source BitBucket\\TintProAPI\\TintProAPI\\Controllers\\InvoiceController.cs:line 159
at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at System.Threading.Tasks.ValueTask`1.get_Result()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
I noticed the following things:
? i.BillTo.CustomerBillTo.Where(x => x.CustomerType == Enum.EnumCustomerType.BillTo).FirstOrDefault().Customer.CustomerId
? i.BillTo.CustomerBillTo.Where(x => x.CustomerType == Enum.EnumCustomerType.BillTo).FirstOrDefault().CustomerId
I'm unable to catch the cause of this problem, please help.
I guess the issue is foreign key is not properly set in the CustomerBillTo entity. Just double check you have done it correctly as bellow.
public class CustomerBillTo
{
[Required]
[ForeignKey("Customer")]
public string CustomerId { get; set; }
public virtual Customer Customer { get; set; }
}
Also since your are accessing as .FirstOrDefault().Customer.CustomerId
. You must have Customer with CustomerBillTo Entity. So make it Required. If not you will have to deal with null reference exception as well.