C# 如何加载所有相关实体?
我想从结果表中加载所有相关实体 当我使用数据集时,我会这样做C# 如何加载所有相关实体?,c#,wpf,linq,entity-framework,C#,Wpf,Linq,Entity Framework,我想从结果表中加载所有相关实体 当我使用数据集时,我会这样做 public static ObservableCollection<ResItem> GetResult(DataSet ds) { ObservableCollection<ResItem> data=new ObservableCollection<ResItem>(); foreach (DataRow dr in ds.Tables["Disc
public static ObservableCollection<ResItem> GetResult(DataSet ds)
{
ObservableCollection<ResItem> data=new ObservableCollection<ResItem>();
foreach (DataRow dr in ds.Tables["Discipline"].Rows)
{
ResItem disc=new ResItem("","","","","","",dr["Discipline"].ToString(),0,0);
foreach (DataRow child1 in dr.GetChildRows("Disc-Test"))
{
ResItem i1=new ResItem("","","","","",child1["TestName"].ToString(),"",0,0);
disc.Items.Add(i1);
foreach (DataRow child2 in child1.GetChildRows("Test-Statement"))
{
foreach (DataRow child3 in child2.GetChildRows("Group-Statement"))
{
ResItem i2 = new ResItem("", "", "", "", child3["GName"].ToString(), "", "",Convert.ToInt32(child2["StatementId"]),0);
i1.Items.Add(i2);
foreach (DataRow child4 in child2.GetChildRows("Statement-Result"))
{
foreach (DataRow child5 in child4.GetChildRows("Student-Result"))
{
ResItem i3 = new ResItem(child4["DatePass"].ToString(),child4["Point"].ToString(), child5["LastName"].ToString(), child5["FirstName"].ToString(), "", "", "",0,Convert.ToInt32(child4["ResultId"]));
i2.Items.Add(i3);
}
}
}
}
}
data.Add(disc);
}
return data;
}
公共静态ObservableCollection GetResult(数据集ds)
{
ObservableCollection数据=新的ObservableCollection();
foreach(ds.表[“规程”].行中的数据行dr)
{
ResItem disc=new ResItem(“,”,“,”,“,”,dr[“规程”]。ToString(),0,0);
foreach(dr.GetChildRows(“磁盘测试”)中的DataRow child1)
{
ResItem i1=新的ResItem(“,”,“,”,“,”,child1[“TestName”]。ToString(),”,0,0);
光盘.项目.添加(i1);
foreach(child1.GetChildRows(“测试语句”)中的datarowchild2)
{
foreach(child2.GetChildRows(“组语句”)中的datarowchild3)
{
ResItem i2=新的ResItem(“,”,“,”,”,child3[“GName”]。ToString(),“,”,Convert.ToInt32(child2[“StatementId”]),0;
i1.项目。添加(i2);
foreach(child2.GetChildRows(“语句结果”)中的datarowchild4)
{
foreach(child4.GetChildRows(“学生结果”)中的datarowchild5)
{
ResItem i3=新的ResItem(child4[“DatePass”].ToString(),child4[“Point”].ToString(),child5[“LastName”].ToString(),child5[“FirstName”].ToString(),“”,“”,0,Convert.ToInt32(child4[“ResultId”]);
i2.项目。添加(i3);
}
}
}
}
}
添加数据(光盘);
}
返回数据;
}
不要告诉我如何使用Linq实现同样的功能。我开始学习实体框架。很抱歉我的英语不好
class ResulViewModel:ViewModelBase
{
readonly Discipline _discipline=new Discipline();
readonly Test _test=new Test();
readonly Group _group=new Group();
readonly Student _student=new Student();
readonly Result _result=new Result();
private string _timePass;
public string Discipline
{
get { return _discipline.Discipline1; }
set { _discipline.Discipline1 = value; RaisePropertyChanged("Discipline"); }
}
public string Test
{
get { return _test.TestName; }
set { _test.TestName = value; RaisePropertyChanged("Test");}
}
public string Group
{
get { return _group.GName; }
set { _group.GName = value; RaisePropertyChanged("Group");}
}
public string Surname
{
get { return _student.LastName; }
set { _student.LastName = value; RaisePropertyChanged("Surname");}
}
public string Name
{
get { return _student.FirstName; }
set { _student.FirstName = value; RaisePropertyChanged("Name");}
}
public string Point
{
get { return _result.Point; }
set { _result.Point = value; RaisePropertyChanged("Point");}
}
public string TimePass
{
get { return _timePass; }
set { _timePass = value; }
}
public ObservableCollection<ResulViewModel> Items { get; set; }
public ResulViewModel(string timePass, string point, string name, string surename, string @group, string test, string discipline)
{
TimePass = timePass;
Point = point;
Name = name;
Surname = surename;
Group = @group;
Test = test;
Discipline = discipline;
Items = new ObservableCollection<ResulViewModel>();
}
}
{
只读规程_规程=新规程();
只读测试_测试=新测试();
只读组_Group=新组();
只读学生_Student=新学生();
只读结果_Result=新结果();
私有字符串\u timePass;
公共弦学科
{
获取{return\u规程1;}
设置{u规程.规程1=value;RaisePropertyChanged(“规程”);}
}
公共字符串测试
{
获取{return\u test.TestName;}
设置{u test.TestName=value;RaisePropertyChanged(“test”);}
}
公共字符串组
{
获取{return\u group.GName;}
设置{u group.GName=value;RaisePropertyChanged(“group”);}
}
公共字符串姓氏
{
获取{return\u student.LastName;}
设置{u student.LastName=value;RaisePropertyChanged(“姓氏”);}
}
公共字符串名
{
获取{return\u student.FirstName;}
设置{u student.FirstName=value;RaisePropertyChanged(“Name”);}
}
公共字符串点
{
获取{return\u result.Point;}
设置{u result.Point=value;RaisePropertyChanged(“Point”);}
}
公共字符串时间传递
{
获取{return\u timePass;}
设置{u timePass=value;}
}
公共ObservableCollection项{get;set;}
公共结果视图模型(字符串时间过程、字符串点、字符串名称、字符串surename、字符串@group、字符串测试、字符串规程)
{
时间通行证=时间通行证;
点=点;
名称=名称;
姓氏=确定姓名;
组=@组;
测试=测试;
纪律=纪律;
Items=新的ObservableCollection();
}
}
您的代码中到底发生了什么?有那么多的
foreach
语句有点不可读。您能给我们看一下ResItem
类和/或数据模型吗?