如何处理 OData Client 中的一对多关系?
How to handle One-to-Many relationship in OData Client?
我有一个 Web Api 2 服务 和一个 WPF OData 客户端,我正在使用 Crystal 报告.
我有两个表(教师,主题)具有一对多他们之间的关系。
我正在尝试让 Subjects 反对 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;
}
但我无法这样做,因为我收到以下 Exception:
An unhandled exception of type 'System.NotSupportedException' occurred in Microsoft.OData.Client.dll
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.
请告诉我该如何解决?
我的问题已解决,我对代码进行了以下更改。
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()
};
我有一个 Web Api 2 服务 和一个 WPF OData 客户端,我正在使用 Crystal 报告.
我有两个表(教师,主题)具有一对多他们之间的关系。 我正在尝试让 Subjects 反对 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;
}
但我无法这样做,因为我收到以下 Exception:
An unhandled exception of type 'System.NotSupportedException' occurred in Microsoft.OData.Client.dll
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.
请告诉我该如何解决?
我的问题已解决,我对代码进行了以下更改。
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()
};