EF Core 5变量let查询异常System.InvalidOperationException: The query contains a projection ‘<>h__Transparen

EF Core 5变量let查询异常

错误消息:

System.InvalidOperationException: The query contains a projection '<>h__TransparentIdentifier3 => DbSet<Cate_img_type_relation>()
    .Where(x => x.Cate_id == <>h__TransparentIdentifier3.<>h__TransparentIdentifier2.<>h__TransparentIdentifier1.<>h__TransparentIdentifier0.d.Id)
    .Select(t => t.Img_type_id)' of type 'IQueryable<string>'. Collections in the final projection must be an 'IEnumerable<T>' type such as 'List<T>'. Consider using 'ToList' or some other mechanism to convert the 'IQueryable<T>' or 'IOrderedEnumerable<T>' into an 'IEnumerable<T>'.

触发查询异常的let语句:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebNetCore5_Img_Storage.Model;
using WebNetCore5_Img_Storage.IDAL;


namespace WebNetCore5_Img_Storage.DAL
{
    /// <summary>
    /// 图册,图册挂在图片活动类型下
    /// </summary>	
    public class Img_categoryDALImpl : DaoBase<Img_category>, IImg_categoryDAL
    {
        /// <summary>
        /// 表联查部门,
        /// </summary>
        /// <returns></returns>
        /// <remarks>
        /// 创建时间:2021-2-1 16:55:04
        /// </remarks>
        public IQueryable<ViewData<Img_category>> JoinDepartment()
        {
            var query = from d in dbContext.Img_category

                            //查询创建人
                        join g in dbContext.User on d.Creator_id equals g.Id into gvv
                        from gv in gvv.DefaultIfEmpty()

                            //查询部门
                        join s in dbContext.Department on d.Department_id equals s.Id into svv
                        from sv in svv.DefaultIfEmpty()

                            //查询图册下图片类型id集合
                        let imgTypeList = from t in dbContext.Cate_img_type_relation.Where(x => x.Cate_id == d.Id) select t.Img_type_id
 
                        select new ViewData<Img_category>()
                        {
                            model = d,
                            //部门id
                            C1 = gv.Department_id,
                            //部门名称
                            C2 = sv.Department_name,
                            //图片类型id集合
                            C9 = imgTypeList.ToList()
                                         
                        };
            return query;

        }
    }
}

解决方案:

错误消息提示let存放的变量应该是IEnumerable <T>类型,比如List<T>,因此let的变量结果值不可以是IQueryable<T>类型

将let修改为直接存放List类型数据,就成功查询了:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebNetCore5_Img_Storage.Model;
using WebNetCore5_Img_Storage.IDAL;


namespace WebNetCore5_Img_Storage.DAL
{
    /// <summary>
    /// 图册,图册挂在图片活动类型下
    /// </summary>	
    public class Img_categoryDALImpl : DaoBase<Img_category>, IImg_categoryDAL
    {
        /// <summary>
        /// 表联查部门,
        /// </summary>
        /// <returns></returns>
        /// <remarks>
        /// 创建时间:2021-2-1 16:55:04
        /// </remarks>
        public IQueryable<ViewData<Img_category>> JoinDepartment()
        {
            var query = from d in dbContext.Img_category

                            //查询创建人
                        join g in dbContext.User on d.Creator_id equals g.Id into gvv
                        from gv in gvv.DefaultIfEmpty()

                            //查询部门
                        join s in dbContext.Department on d.Department_id equals s.Id into svv
                        from sv in svv.DefaultIfEmpty()

                            //查询图册下图片类型集合,会导致异常System.InvalidOperationException: The query contains a projection '<>h__TransparentIdentifier3 => DbSet<Cate_img_type_relation>()
                            // let imgTypeList = from t in dbContext.Cate_img_type_relation.Where(x => x.Cate_id == d.Id) select t.Img_type_id

                            //查询成功
                        let imgTypeList = dbContext.Cate_img_type_relation.Where(x => x.Cate_id == d.Id).Select(x => x.Img_type_id).ToList()

                        select new ViewData<Img_category>()
                        {
                            model = d,
                            //部门id
                            C1 = gv.Department_id,
                            //部门名称
                            C2 = sv.Department_name,
                            //图片类型id集合
                            //C9 = imgTypeList.ToList()
                             C9 = imgTypeList                             
                        };
            return query;

        }
    }
}

ViewData

  /// <summary>
    /// 表联查显示,扩展字段显示,适用于IQueryable<T> 返回数据,
    /// 当主要使用一个表字段,联查的其他表字段较少时,比较适用
    /// </summary>
    /// <typeparam name="T">泛型</typeparam>
    public class ViewData<T>
    {
        /// <summary>
        /// 实体
        /// </summary>
        public T model { get; set; }

        /// <summary>
        /// 扩展字段
        /// </summary>
        public string C1 { get; set; }

        /// <summary>
        /// 扩展字段
        /// </summary>
        public string C2 { get; set; }
        public string C3 { get; set; }
        public string C4 { get; set; }
        public string C5 { get; set; }
        public string C6 { get; set; }
        public string C7 { get; set; }
        public string C8 { get; set; }
        public List<string> C9 { get; set; }      
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王焜棟琦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值