【问题标题】:DbExpressionBinding requires an input expression with a collection ResultType. Parameter name: inputDbExpressionBinding 需要一个带有集合 ResultType 的输入表达式。参数名称:输入
【发布时间】:2016-07-19 08:42:05
【问题描述】:

我的 linq 查询有一个大问题

            var t = from tl in _GxEntities.T_L
                             join td in _GxEntities.T_D on (int)tl.fld_rdRequestfk equals td.fld_rqID_pk
                             join tpd in _GxEntities.T_P_D on (int)td.fld_rqID_pk equals tpd.fld_pdRequest_fk                                 
                             where tpd.fld_pdda == null
                                && tpd.fld_pdSU_fk == tl.fld_rdSU_fk
                                && td.fld_rqstatus != 1
                                && tl.fld_rcid != null
                                && tl.loc_id == UsrProfilService.loc_id
                                && tl.fld_rcid.Any(x=> tblRvGxFr.Select(c=>c.Nte).Contains(x.ToString()))
                             group tl by new
                             {
                                 tl.fld_rcid,
                                 tl.fld_rdSU_fk,
                                 tl.fld_rdtRequested_Q,
                                 tl.loc_id
                             } into q
                             select new RVSyntheseDetailModel()
                             {
                                 RC_ID = q.Key.fld_rcid,
                                 SU = (int)q.Key.fld_rdSU_fk,
                                 Ts = (int)q.Sum(k => k.fld_rdtRequested_Q),
                                 LOC_ID = (short)q.Key.loc_id
                             };

        return t.ToList();

如果我评论&& tl.fld_rcid.Any(x=> tblRvGxFr.Select(c=>c.Nte).Contains(x.ToString()))这部分我没有任何问题。 tblRvGxFr 来自另一个实体,看起来像这样:

var dateLimit = DateTime.Now.Date.AddMonths(-1);

var tblRtvGrxFr = from tgx in _FtEntities.TL_RV_FR_GX
 where tgx.RV_D_LIM_RET > dateLimit && tgx.RV_N_IDT_DAP != null && tgx.RV_LOC_ID == SqlFunctions.StringConvert((double)UsrProfilService.loc_id)
  select new RVSyntheseRrModel()
  {
      Nte = tgx.RV_ETET,
      Tr = tgx.RV_TR,
      Filiere = tgx.RV_T_PRD,
      Lib_art = tgx.RV_L_ART,
      SU = tgx.RV_N_IDT_DAP,
      Qte_demand = (int?)tgx.RT_Q_DEM_RET ?? 0,
      Ord = 0,
      Tcs = 0,
      AfA = 0,
      PrA = 0,
      Exped = 0,
      Loc_Id = tgx.RV_LOC_ID,
      Reason = tgx.RV_REASON
  };

你能帮帮我吗?为什么我会收到此错误“DbExpressionBinding 需要具有集合 ResultType 的输入表达式。参数名称:输入”?

如果我将此条件更改为:&& tblRvGxFr.Select(i => i.Nte).Contains(tl.fld_rcid) 我有此错误:无法处理类型“GX.ViewModels.RVSyntheseRrModel[]” ,因为它没有到值层的已知映射。

【问题讨论】:

  • fld_rcid 字段的类型是什么?这也是 LINQ to SQL 还是 LINQ to Entities?
  • fld_rcid 是一个字符串,这是 Linq to Entities
  • 好的。你想达到什么目的? (A) fld_rcid 包含任何nte,(B) 任何nte 等于fld_rcid (C) 任何nte 包含fld_rcid
  • 我想得到所有在“nte”中的“fld_rcid”
  • (C) 任何 nte 包含 fld_rcid

标签: c# linq


【解决方案1】:

通常你会使用这样的东西

&& tblRvGxFr.Any(c => c.Nte.Contains(tl.fld_rcid))

但是 LINQ to Entities 不支持涉及不同 DbContexts 的查询。所以你需要将Ntes 抓取到本地列表/数组中

var nteList = tblRvGxFr.Select(c => c.Nte).ToList();

然后改用这个:

&& nteList.Any(nte => nte.Contains(tl.fld_rcid))

希望Nte 列表不要太大,否则SQL 查询会很大且效率低下。但 AFAIK 不存在其他多上下文解决方案。

【讨论】:

  • 感谢您的帮助。 Nte 是一个很大的列表
  • 太糟糕了。有机会使用同一个DbContext吗?
  • 不,这是不可能的...在此之前我使用的是视图,但加载速度太慢,这就是为什么我的老板让我用 linq 对其进行编码...现在我正在尝试团结所有子查询,我正在与实体框架的限制作斗争:(
猜你喜欢
  • 1970-01-01
  • 2014-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-31
  • 1970-01-01
相关资源
最近更新 更多