Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF6 exception: DbExpressionBinding requires an input expression with a collection ResultType

I'm facing an exception when I run this query (using LinqPad for debugging):

int[] serviceCodes= new int[] { 1610, 1611, 1612 };
byte[] payModes = new byte[] { 1, 2 };
int[] months = new int[] { 10, 11, 12 };
int year = 2017;

using (var context = new FinanceConnection())
{
   var result = from a in
            (from a in context.BILL_INFO_DETAILS
             where
                 a.INPUT_STATUS == true &&
                 serviceCodes.Contains(a.SERVICE_INFO.SERVICE_CODE) &&
                 payModes.Contains(a.PAY_MODE_ID) &&
                 a.STAMP_DATE != null &&
                 months.Contains(a.STAMP_DATE.Value.Month) &&
                 a.STAMP_DATE.Value.Year == year &&
                 a.SERVICE_INFO.FEE > 1
             select new
             {
                 a.REQUESTED_QTY,
                 a.ITEM_TOTAL,
                 Dummy = "x"
             })
             group a by new { a.Dummy }
        into g
             select new ViewGuessAlMajlisOffline
             {
                 Transaction =
                     g.Sum(p => p.REQUESTED_QTY) == 0 ? (int?)null : (int)g.Sum(p => p.REQUESTED_QTY),
                 Income = g.Sum(p => p.ITEM_TOTAL) == 0
                     ? (decimal?)null
                     : (decimal)g.Sum(p => p.ITEM_TOTAL)
             }; 

    result.Dump();
}

I have searched the SO questions with the same titles but my contain lists are simple arrays so I don't know what is actually causing the exception.

Any pointers are highly appreciated.

Update

I have tried removing the two .Contains() in where and the query works. Actually, commenting only the payModes.Contains(a.PAY_MODE_ID) makes the query work

Update

public partial class BILL_INFO_DETAIL : DA.Services.IBS.Data.EntityFramework.Helper.IBaseEntity
{
    public string BILL_NO { get; set; }
    public byte PAY_MODE_ID { get; set; }
    public int CASHIER_NO { get; set; }
    public int SERVICE_CODE { get; set; }
    public Nullable<int> REQUESTED_QTY { get; set; }
    public Nullable<int> CURRENT_QTY { get; set; }
    public Nullable<decimal> FEE { get; set; }
    public Nullable<decimal> ITEM_TOTAL { get; set; }
    public Nullable<decimal> VAT_AMOUNT { get; set; }
    public string USER_ID { get; set; }
    public Nullable<int> BUSINESS_USER_ID { get; set; }
    public Nullable<bool> INPUT_STATUS { get; set; }
    public Nullable<System.DateTime> STAMP_DATE { get; set; }

    public virtual BUSINESS_USER BUSINESS_USER { get; set; }
    public virtual CASHIER CASHIER { get; set; }
    public virtual PAY_MODE PAY_MODE { get; set; }
    public virtual SERVICE_INFO SERVICE_INFO { get; set; }
}
like image 521
Hassan Gulzar Avatar asked Jan 02 '23 17:01

Hassan Gulzar


1 Answers

There seem to be a bug (in both EF6.1.3 and 6.2) with Contains method translation when applied on byte array (probably because usually the byte arrays are used to represent binary data).

The workaround is to use int array:

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

or explicit enumerable (to avoid byte[] special processing):

var payModes = new byte[] { 1, 2 }.AsEnumerable();

Note that the conversion to enumerable should be outside the query expression tree, because AsEnumerable() call is not recognized by the EF query translator and will generate NotSupportedException.

like image 152
Ivan Stoev Avatar answered Jan 29 '23 08:01

Ivan Stoev