Linq Result进入Observable集合会引发解析异常

时间:2020-03-19 10:17:58

标签: c# linq

我创建了一个可观察的集合

 public ObservableCollection<DatagGridCollection> combine_audit_final_collection { get; set; }

我正试图通过linq结果填充

  var d =  (from p in Auditcollectiondata
                           from c in Finalcollectiondata
                           where c.sno == p.sno
                           select new
                            {
                                p.sno,
                                p.AuditID,
                                p.claimnumber,
                                p.QueryID,
                                p.DateWorked,
                                p.UserID,
                                p.Line,
                                p.Dos,
                                p.CPT,
                                p.Units,
                                p.amtBilled,
                                p.RecoupUnit,
                                p.reocupamts,
                                p.ScioNote,
                                p.Linelevelstatus_valuetext,
                                p.providerNote,
                                c.ID_finalstatus,
                                c.FinalStatus
                     });

联接工作正常,但是当我尝试将结果插入可观察的集合中时。我遇到了投放错误。

    combine_audit_final_collection = new ObservableCollection<DatagGridCollection>((ObservableCollection<DatagGridCollection>) d);

combine_audit_final_collection将被绑定到数据网格中。尽管没有编译错误,但我在执行时却在运行时解析了异常。

更新:我尝试使用sno加入两个可观察的集合,并将结果插入到另一个可观察的集合'combine_audit_final_collection'中。如果我的方法有误,请让我知道其他方法。

public class DatagGridCollection
{
   public bool update { get; set; }
   public int sno { get; set; }
   public string AuditID { get; set; }
   public string claimnumber { get; set; }
   public string QueryID { get; set; }
   public DateTime DateWorked { get; set; }
   public string UserID { get; set; }
   public string Line { get; set; }
   public string Dos { get; set; }
   public string CPT { get; set; }
   public string Units { get; set; }
   public string amtBilled { get; set; }
   public string RecoupUnit { get; set; }
   public string reocupamts { get; set; }
   public string ScioNote { get; set; }
   public string Linelevelstatus_valuetext { get; set; }
   public string providerNote { get; set; }
   public int final_status_sno { get; set; }
   public string Finalstatus { get; set; }
}

1 个答案:

答案 0 :(得分:1)

您确定您的ObservableCollection是DataGridCollections的集合吗?集合中的每个元素都是DataGridCollection吗?

如果不是,但实际上是MyType的集合,请在下面用DataGridCollection更改单词MyType

无论如何,如果您要在调试器中检入对象d的类型,则会注意到它不是IEnumerable<DatagGridCollection>

只需将代码更改为:

select new DataGridCollection()
{
   p.sno,
   ...

如果您想在将来发现这种错误的原因,我的建议是不要过多使用var一词,也不要一次执行太多语句。

IEnumerable<DataGridCollection> d = ...
    Select new DataGridCollection
    {
        ...
    };

combine_audit_final_collection = new ObservableCollection<DatagGridCollection>(d);

查找错误会容易得多。