不支持查询运算符“ToDictionary”

时间:2016-09-15 05:53:06

标签: c# dictionary linq-to-sql

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

 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'

我在这里出错了。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

此处的问题是您尝试在.ToDictionary(id => id.str_Panel, id => id.lng_Count) })内调用IQueryable<T>,这是在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();