如何加载所有相关实体?

时间:2014-11-05 03:22:14

标签: c# wpf linq entity-framework

我想从Results表中加载所有相关实体 当我使用数据集时,我这样做

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

不要告诉我如何使用Linq.I开始学习Entity Framework.Sorry for my bad English

1 个答案:

答案 0 :(得分:-1)

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