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()
    .Where(x => x.Cate_id == <>h__TransparentIdentifier3.<>h__TransparentIdentifier2.<>h__TransparentIdentifier1.<>h__TransparentIdentifier0.d.Id)
    .Select(t => t.Img_type_id)' of type 'IQueryable'. Collections in the final projection must be an 'IEnumerable' type such as 'List'. Consider using 'ToList' or some other mechanism to convert the 'IQueryable' or 'IOrderedEnumerable' into an 'IEnumerable'.

触发查询异常的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
{
    /// 
    /// 图册,图册挂在图片活动类型下
    /// 	
    public class Img_categoryDALImpl : DaoBase<Img_category>, IImg_categoryDAL
    {
        /// 
        /// 表联查部门,
        /// 
        /// 
        /// 
        /// 创建时间:2021-2-1 16:55:04
        /// 
        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
{
    /// 
    /// 图册,图册挂在图片活动类型下
    /// 	
    public class Img_categoryDALImpl : DaoBase<Img_category>, IImg_categoryDAL
    {
        /// 
        /// 表联查部门,
        /// 
        /// 
        /// 
        /// 创建时间:2021-2-1 16:55:04
        /// 
        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()
                            // 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

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

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

        /// 
        /// 扩展字段
        /// 
        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; }      
    }

你可能感兴趣的:(EF,Core,EF,Core,EF,Core5,c#)