如何处理OData客户端中的一对多关系?

时间:2015-10-09 08:38:27

标签: c# wpf crystal-reports odata asp.net-web-api2

我有 Web Api 2服务和WPF OData客户端,我正在使用 Crystal Reports

我有两张桌子(教师主题),他们之间有一对多关系。 我正在尝试主题反对 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.NotSupportedException'发生在Microsoft.OData.Client.dll

中      

附加信息:构建或初始化实例   类型   <> f__AnonymousType1`9 [System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,QuizSystemClient.RestApiOData.Models.Subject]   表达式为c.Subjects.Where(s =>(s.Teacher_Id ==   c.Teacher_Id))。不支持SingleOrDefault()。

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

1 个答案:

答案 0 :(得分:0)

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

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