不支持查询运算符 'ToDictionary'

The query operator 'ToDictionary' is not supported

我正在使用以下查询来获取字典:

 using (JSEADataContext dc = new JSEADataContext(JSEADBContext.GetConnectionstring()))
                {
                   var Incident = (from incident in dc.Incidents
                                      from main in dc.Mains.Where(inv => inv.ReportID == incident.reportid).DefaultIfEmpty() 
                                      from teamApproval in dc.TeamsAndApprovals.Where(inv => inv.ReportID == incident.reportid).DefaultIfEmpty() 
                                      where incident.reportid == "123123"
                                      orderby incident.reportid ascending
                                      select new Data
                                      {
      AssessmentFormCount = (from assessmentCount in dc.Table1
                                                                 join panel in dc.Table2 on assessmentCount.lng_ID equals panel.lng_id
                                                                 into temp
                                                                 from j in temp.DefaultIfEmpty()
                                                                 where assessmentCount.ReportID == main.ReportID
                                                                 group j by j.str_desc into grouping
                                                                 select new AssessmentFormCheckedCount
                                                                     {
                                                                         str_Panel = grouping.Key,
                                                                         lng_Count = grouping.Count()
                                                                     }).AsEnumerable().ToDictionary(id => id.str_Panel, id => id.lng_Count) }).ToList<Data>().SingleOrDefault();
}



 public class AssessmentFormCheckedCount
    {
        public string str_Panel { get; set; }
        public int lng_Count { get; set; }
    }

 public class Data : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
       #region Assessment Count
        private AssessmentFormCheckedCount _AssessmentFormCount { get; set; }
        public AssessmentFormCheckedCount AssessmentFormCount
        {
            get
            {
                return this._AssessmentFormCount;
            }
            set
            {
                if ((this._AssessmentFormCount != value))
                {

                    this._AssessmentFormCount = value;
                    this.NotifyPropertyChanged("AssessmentFormCount");

                }
            }
        }
        #endregion
}

问题:

当我尝试执行此操作时,我得到了

The query operator 'ToDictionary' is not supported

我哪里错了。

现在我正在调用一种方法而不是查询,它包含相同的查询并且现在工作正常。

using (JSEADataContext dc = new JSEADataContext(JSEADBContext.GetConnectionstring()))
                {
                   var Incident = (from incident in dc.Incidents
                                      from main in dc.Mains.Where(inv => inv.ReportID == incident.reportid).DefaultIfEmpty() 
                                      from teamApproval in dc.TeamsAndApprovals.Where(inv => inv.ReportID == incident.reportid).DefaultIfEmpty() 
                                      where incident.reportid == "123123"
                                      orderby incident.reportid ascending
                                      select new Data
                                      {
      AssessmentFormCount = test(main.str_ReportID, dc) }).ToList<Data>().SingleOrDefault();
}




 private Dictionary<string, int> test(string ReportID, DataContext dc)
    {
        var ceck = (from assessmentCount in dc.AssessmentForms.AsEnumerable()
                    join panel in dc.masters.AsEnumerable() on assessmentCount.lng_ID equals panel.lng_id
                    into temp
                    from j in temp.DefaultIfEmpty()
                    where assessmentCount.ReportID == ReportID
                    group j by j.desc into grouping
                    select new AssessmentFormCheckedCount
                        {
                            str_Panel = grouping.Key,
                            lng_Count = grouping.Count()
                        }).ToList();
        var t = ceck.ToDictionary(p => p.str_Panel, p => p.lng_Count);
        return t;
    }

这里的问题是您试图在 IQueryable<T> 中调用 .ToDictionary(id => id.str_Panel, id => id.lng_Count) }),即 SQL 服务器上的 运行,但没有将 ToDictionary 转换为 SQL 命令的方法。

因此,即使它是有效的 C# 代码,它也无法转换为有效的 SQL。您需要做的是确保您的 IQueryable<T> 可以转换为 SQL,然后对结果调用 .ToList()(或 .ToArray())以将其拉入内存.然后你可以对结果调用.ToDictionary(...)

像这样:

var Incident =
(
    from incident in dc.Incidents
    from main in dc.Mains.Where(inv => inv.ReportID == incident.reportid).DefaultIfEmpty()
    from teamApproval in dc.TeamsAndApprovals.Where(inv => inv.ReportID == incident.reportid).DefaultIfEmpty()
    where incident.reportid == "123123"
    orderby incident.reportid ascending
    select
    (
        from assessmentCount in dc.Table1
        join panel in dc.Table2 on assessmentCount.lng_ID equals panel.lng_id
        into temp
        from j in temp.DefaultIfEmpty()
        where assessmentCount.ReportID == main.ReportID
        group j by j.str_desc into grouping
        select new
        {
            str_Panel = grouping.Key,
            lng_Count = grouping.Count()
        }
    )
).Take(1).ToList();

var result =
    Incident
        .Select(xs => xs.ToDictionary(id => id.str_Panel, id => id.lng_Count))
        .ToList();