我有 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()。
请告诉我如何解决这个问题?
答案 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()
};