简体   繁体   English

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

[英]How to handle One-to-Many relationship in OData Client?

I have a Web Api 2 Service and a WPF OData Client and I am working with Crystal Reports . 我有一个Web Api 2服务和一个WPF OData客户端,并且正在使用Crystal Reports

I have two tables( Teacher , Subject ) having One-to-Many relationship between them. 我有两个表( TeacherSubject ),它们之间具有一对多的关系。 I am trying to get Subjects against Teacher_Id . 我正在尝试针对Teacher_Id获取主题

Following is the code that I am using: 以下是我正在使用的代码:

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

But I am unable to do so as I am getting the Following Exception : 但是由于出现以下异常而无法这样做:

An unhandled exception of type 'System.NotSupportedException' occurred in Microsoft.OData.Client.dll Microsoft.OData.Client.dll中发生了类型为'System.NotSupportedException'的未处理的异常

Additional information: Constructing or initializing instances of the type <>f__AnonymousType1`9[System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,QuizSystemClient.RestApiOData.Models.Subject] with the expression c.Subjects.Where(s => (s.Teacher_Id == c.Teacher_Id)).SingleOrDefault() is not supported. 附加信息:构造或初始化类型为<> f__AnonymousType1`9 [System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,QuizSystemClient的实例。不支持带有表达式c.Subjects.Where(s =>(s.Teacher_Id == c.Teacher_Id))。SingleOrDefault()的RestApiOData.Models.Subject]。

Please tell me how can I resolve that? 请告诉我该如何解决?

My problem is solved, I had made the following changes in my code. 我的问题解决了,我在代码中做了以下更改。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM