EF6例外:DbExpressionBinding需要具有集合ResultType的输入表达式 [英] EF6 exception: DbExpressionBinding requires an input expression with a collection ResultType

查看:67
本文介绍了EF6例外:DbExpressionBinding需要具有集合ResultType的输入表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行此查询时遇到异常(使用LinqPad进行调试):

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

我用相同的标题搜索了SO问题,但是我的包含列表是简单的数组,所以我不知道实际上是什么导致异常.

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.

更新

我尝试删除其中和查询有效的两个.Contains().实际上,仅注释payModes.Contains(a.PAY_MODE_ID)可使查询正常工作

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

更新

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

推荐答案

byte数组上应用Contains方法转换时似乎存在一个错误(在EF6.1.3和6.2中).字节数组用于表示二进制数据.

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).

解决方法是使用int数组:

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

或显式可枚举(以避免byte[]特殊处理):

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

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

请注意,对枚举的转换应在查询表达式树之外,因为EF查询翻译器无法识别AsEnumerable()调用,并且会生成NotSupportedException.

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.

这篇关于EF6例外:DbExpressionBinding需要具有集合ResultType的输入表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆