locked
How to cast all child elements in an unlimited hierachical data ?

    Question

  • This is how my class looks like

    public string Id { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public string StudentNo { get; set; }    
    public object Subjects { get; set; }

    and i want to cast all Subjects object to a newly created Subject class

    How can i achieve this ?


    19karabo91

    Tuesday, February 24, 2015 6:47 AM

Answers

  • The question is what is the type of subjects?

    is it an array or a list, or something else

    you may use 'as' to cast back to the type it really is.

    for example

    Subject[] subjects=theObject.Subjects as Subject[]

    You need to have the knowledge what type the original subjects is and what data you stored there before you can convert it to ur own class by a constructor

    class MySubject
    {
    public MySubject(OtherSubject subject)
    {
     this.Title=subject.Title;
     this.Code=subject.Id;
    }
    }

    Tuesday, February 24, 2015 6:54 AM
  • Hi karabo,

    I agree with hereafter about using the as keyword, cast Subjects property to an array and then you can use LINQ to do any queries. Code looks like the following.

    Subject sub = new Subject();
    
                var array = sub.Subjects as List<SomeClass>;
    
                if (array != null)
    
                {
    
    
                }
    
    class Subject
    
        {
    
            public string Id { get; set; }
    
            public string Name { get; set; }
    
            public string Surname { get; set; }
    
            public string StudentNo { get; set; }
    
            public object Subjects { get; set; }
    
    }
    

    If I have any misunderstanding, please post more information about your scenario.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, March 3, 2015 5:28 AM
    Moderator

All replies

  • The question is what is the type of subjects?

    is it an array or a list, or something else

    you may use 'as' to cast back to the type it really is.

    for example

    Subject[] subjects=theObject.Subjects as Subject[]

    You need to have the knowledge what type the original subjects is and what data you stored there before you can convert it to ur own class by a constructor

    class MySubject
    {
    public MySubject(OtherSubject subject)
    {
     this.Title=subject.Title;
     this.Code=subject.Id;
    }
    }

    Tuesday, February 24, 2015 6:54 AM
  • Subject class contain a list of subSubject classes as children of Subject that i need to filter through .

    19karabo91

    Tuesday, February 24, 2015 9:32 AM
  • Hi karabo,

    I agree with hereafter about using the as keyword, cast Subjects property to an array and then you can use LINQ to do any queries. Code looks like the following.

    Subject sub = new Subject();
    
                var array = sub.Subjects as List<SomeClass>;
    
                if (array != null)
    
                {
    
    
                }
    
    class Subject
    
        {
    
            public string Id { get; set; }
    
            public string Name { get; set; }
    
            public string Surname { get; set; }
    
            public string StudentNo { get; set; }
    
            public object Subjects { get; set; }
    
    }
    

    If I have any misunderstanding, please post more information about your scenario.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, March 3, 2015 5:28 AM
    Moderator