如何处理OData Client中的一对多关系

本文关键字:Client 一对多 关系 OData 何处理 处理 | 更新日期: 2023-09-27 18:17:04

我有一个Web Api 2服务和一个WPF OData客户端,我正在使用水晶报告

我有两个表(TeacherSubject),它们之间具有一对多关系。我正试图获得科目Teacher_Id

下面是我使用的代码:

private string ServiceUri = "http://localhost:50623/odata";
    private void pge_TeacherReportPage_Loaded(object sender, RoutedEventArgs e)
    {
        var Container = new Default.Container(new Uri(ServiceUri));
        ReportDocument report = new ReportDocument();
        report.Load("../../TeacherCrystalReport.rpt");
        var Teacher = from c in Container.Teachers
                      select new
                      {
                         Name = c.Name,
                         FatherName = c.FatherName,
                         ContactNo = c.ContactNo,
                         Address = c.Address,
                         Religion = c.Religion,
                         CNIC = c.CNIC,
                         Status = c.Status,
                         UserName = c.UserName,
                         Subjects = c.Subjects.Where(s=> s.Teacher_Id == c.Teacher_Id).SingleOrDefault()
                      };
        report.SetDataSource(Teacher);            
        rpt_Teacher.ViewerCore.ReportSource = report;
    }

但是我无法这样做,因为我得到以下异常:

类型为"System"的未处理异常。在Microsoft.OData.Client.dll中出现了NotSupportedException

类的实例的构造或初始化类型& lt;> f__AnonymousType1 9 (system . string, system . string, system . string, system . string, System.String, System.String, System.String, System.String, QuizSystemClient.RestApiOData.Models.Subject]c.主语。Where(s => (s. teacher_id == ?c.Teacher_Id)). singleordefault()不支持

请告诉我如何解决这个问题?

如何处理OData Client中的一对多关系

我的问题解决了,我在代码中做了以下更改:

    var Teacher = from c in Container.Teachers
                      select new
                      {
                         Name = c.Name,
                         FatherName = c.FatherName,
                         ContactNo = c.ContactNo,
                         Address = c.Address,
                         Religion = c.Religion,
                         CNIC = c.CNIC,
                         Status = c.Status,
                         UserName = c.UserName,
                         //I had made changes in the following code:
                         Subjects = c.Subjects.Select(s=>s.Subject_Name).FirstOrDefault()
                      };