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

那年仲夏 提交于 2019-12-23 16:49:15

问题


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; }
}

回答1:


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.



来源:https://stackoverflow.com/questions/49123858/ef6-exception-dbexpressionbinding-requires-an-input-expression-with-a-collectio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!