查询操作符'ToDictionary'不支持

本文关键字:不支持 ToDictionary 操作符 查询 | 更新日期: 2023-09-27 18:07:08

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

 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
}

问题:

当我试图执行这个时,我得到

不支持查询操作符'ToDictionary'

我在哪里做错了。

查询操作符'ToDictionary'不支持

这里的问题是,您试图在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(); 

现在我正在调用一个方法,而不是包含相同查询的查询,它现在工作得很好。

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;
    }