无法将类型IQueryable隐式转换为类

时间:2019-09-12 18:27:07

标签: c# linq class iqueryable

我正在尝试将多个结果放入列表中,以发送回JS来填充网格。第一个查询(_mappedQuery)正在获取数据。然后,我想结束将值放入_udfList对象。我不断收到关于“无法将查询转换为类”错误的信息

我尝试将其设置为列表,创建查询对象,单个类对象。到目前为止一切都不顺利

            MemberMNCFormsList _udfList = new MemberMNCFormsList();

            foreach (var _row in _udfTables) {
                System.Diagnostics.Debugger.Break();
                System.Diagnostics.Debugger.Launch();

                var _mappedQuery = (from res in Query<UdfColumnMapping>().AsNoTracking()
                                    join udf in Query<UserDefinedForms>().AsNoTracking() on res.Func_Area equals udf.Func_Area
                                    join ds in Query<Data_Set>().AsNoTracking() on res.Data_ID equals ds.DATA_ID
                                    join df in Query<DEFITEM>().AsNoTracking() on ds.DEF_ID equals df.DEF_ID
                                    where udf.UserDefinedForms_ID == _row.UserDefinedForms_ID &&
                                     (res.FieldName.ToLower().StartsWith("reviewname") ||
                                     res.FieldName.ToLower().StartsWith("disposition") ||
                                     res.FieldName.ToLower().StartsWith("reviewdate"))
                                    select (new MemberMNCForms {
                                        UserDefinedFormData_ID = _row.UserDefinedFormData_ID,
                                        FormId = udf.UserDefinedForms_ID,
                                        MappedColumnName = res.MappedColumnName,
                                        FieldName = res.FieldName,
                                        MappedTableName = res.MappedTableName,
                                        Reviewed_Name = _row.LAST_NAME.Trim() + ", " + _row.FIRST_NAME.Trim(),
                                        Reviewed_Date = _row.CreateDate.GetShortDateorEmpty().ToString()
                                    }));

                var _formRow = _mappedQuery.Select(t => new MemberMNCForms {
                    UserDefinedFormData_ID = t.UserDefinedFormData_ID,
                    FormId = t.FormId,
                    MappedColumnName = t.MappedColumnName,
                    FieldName = t.FieldName,
                    MappedTableName = t.MappedTableName,
                    Reviewed_Name = t.Reviewed_Name,
                    Reviewed_Date = t.Reviewed_Date
                })));

                _udfList.list.Add(_formRow);

   public sealed class MemberMNCForms {
        public Guid? UserDefinedFormData_ID { get; set; }
        public int FormId { get; set; }
        public string Reviewed_Name { get; set; }
        public string MappedColumnName { get; set; }
        public string FieldName { get; set; }
        public string MappedTableName { get; set; }
        public int? MNCDetermination_ID { get; set; }
        public string Reviewed_By { get; set; }
        public string Reviewed_Date { get; set; }
    }

    public sealed class MemberMNCFormsList : ErrorInfo
    {
        public List<MemberMNCForms> list = new List<MemberMNCForms>();
        public int Count { get; set; }
    }

我试图用来自_mappedQuery的值填充_udfList对象。我认为唯一可行的方法是为_mappedQuery中的每个记录创建一个MemberMNCForms对象,然后将其添加到_udfList.list

2 个答案:

答案 0 :(得分:1)

以这种方式使用linq时,即使列表中有Select被使用的单个项目,它也只是一个表达式树,直到被迭代为止。

我假设您期望返回MemberMNCForms的集合,因此您需要使用AddRange而不是Add

_udfList.list.AddRange(_formRow);

要确保已执行,可以使用ToList

_udfList.list.AddRange(_formRow.ToList());

如果只是期望得到一个结果,则可以使用SingleOrDefault

var result = _formRow.SingleOrDefault();
if (result != null) {
    _udfList.list.Add(result);
}

答案 1 :(得分:0)

_formRowIEnumerable<MemberMNCForms>

var _formRow = _mappedQuery.Select(t => new MemberMNCForms {
    UserDefinedFormData_ID = t.UserDefinedFormData_ID,
    FormId = t.FormId,
    MappedColumnName = t.MappedColumnName,
    FieldName = t.FieldName,
    MappedTableName = t.MappedTableName,
    Reviewed_Name = t.Reviewed_Name,
    Reviewed_Date = t.Reviewed_Date
})));

在这里,您尝试将IEnumerable<MemberMNCForms>添加到List<MemberMNCForms>

_udfList.list.Add(_formRow);

您无法使用.Add执行此操作。您必须使用.AddRange

尝试一下:

_udfList.list.AddRange(_formRow);