none
WCF Data Service | One-to-many relationships RRS feed

  • Question

  • This is my datamodel class

    namespace WcfrestService { [DataContract] public class Employee { [DataMember] public int EmpNo { get; set; } [DataMember] public string EmpName { get; set; } public List<Department> AllDepart { get; set; } [DataMember] public string DeptName { get; set; } }

    [DataContract]
        public class Sales
        {
            [DataMember]
            public string Name { get; set; }
    
            [DataMember]
            public string pname { get; set; }
    }

    [DataContract]

    public class Department { [DataMember] public int DeptNo { get; set; } [DataMember] public string Dname { get; set; } [DataMember] public string Location { get; set; } //public List<Employee> Allempl { get; set; } public Employee Employee { get; set; } } public partial class EmployeeData { private static readonly EmployeeData _instance = new EmployeeData(); private EmployeeData() { } public static EmployeeData Instance { get { return _instance; } } List<Employee> empList = new List<Employee> { new Employee() { EmpNo=101,EmpName="Anil",DeptName="DEV", AllDepart=new List<Department> { new Department (){ DeptNo=1,Dname="Sales",Location="Vij",

    new List<Sales> { new Sales (){Nname="jjjj",pname="ggg",

    }}}}

    }; public List<Employee> EmployeeList { get { return empList; } } } }

     [ServiceContract]
        public interface IService
        {        
            [WebGet(UriTemplate = "Employee", ResponseFormat = WebMessageFormat.Xml)]
            [OperationContract]
            List<Employee> GetAllEmployeeDetails();
    }
    Implementation service
    
     public List<Employee> GetAllEmployeeDetails()
            {
                return EmployeeData.Instance.EmployeeList;
            }
    
    

    I am trying to display data from both the class but it's getting only single class only

    I want to out putlike below

    <Employee>

    <EmpNo>1</EmpNo>

    <Ename>Anil</Ename>

    <Department>

         <DeptNo>

        <DeptNo>10</DeptNo>

        <Dname>Sales</Dname>

        <Location>Vij</Location>

    </Department>

    <DeptName>DEV</DeptName>

    </Employee>

    I want to above output,

    How can i get that type of output?


    anilbabu



    • Edited by anil33 Wednesday, July 9, 2014 2:42 AM
    Monday, July 7, 2014 4:43 PM

Answers

  • Hi,

    I see that you have edited your post, then for the edited post, please try to modify your code as following:

         [ServiceContract]
        public interface IService
        {        
            [WebGet(UriTemplate = "Employee", ResponseFormat = WebMessageFormat.Xml)]
            [OperationContract]
            List<Employee> GetAllEmployeeDetails();
    }
        // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
        [DataContract]
        public class Employee
        {
            [DataMember(Order = 4)]
            public int EmpNo { get; set; }
    
            [DataMember(Order = 1)]
            public string EmpName { get; set; }
            [DataMember(Order = 3)]
            public List<Department> AllDepart { get; set; }
            [DataMember(Order=0)]
            public string DeptName { get; set; }
            [DataMember(Order = 2)]
            public Sales Sales { get; set; }
            
        }
    
    
    [DataContract]
        public class Sales
        {
            [DataMember]
            public string Name { get; set; }
    
            [DataMember]
            public string pname { get; set; }
    }
        [DataContract]
    
    
        public class Department
        {
            [DataMember]
            public int DeptNo { get; set; }
    
            [DataMember]
            public string Dname { get; set; }
    
            [DataMember]
            public string Location { get; set; }
            //public List<Employee> Allempl { get; set; }
    
           
        }
        public partial class EmployeeData
        {
            private static readonly EmployeeData _instance = new EmployeeData();
    
            private EmployeeData() { }
    
            public static EmployeeData Instance
            {
                get
                {
                    return _instance;
                }
            }
    
            List<Employee> empList = new List<Employee>
            {
                      new Employee() { EmpNo=101,EmpName="Anil",DeptName="DEV", 
                        AllDepart=new List<Department> { new Department (){ DeptNo=1,Dname="Sales",Location="Vij"},new Department (){ DeptNo=2,Dname="DEv",Location="Gun"},
       new Department (){ DeptNo=3,Dname="pro",Location="Ong"}},
    
                                          Sales=new Sales (){Name="jjjj",pname="ggg"}}
    
                        };
    
    
          
    
    
            public List<Employee> EmployeeList
            {
                get
                {
                    return empList;
                }
            }
            
        }
    Then result:


    Best Regards,
    Amy Peng

    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.

    • Marked as answer by anil33 Friday, July 11, 2014 1:41 PM
    Friday, July 11, 2014 2:58 AM
    Moderator

All replies

  • [DataContract]
       
    public class Employee
       
    {
           
    [DataMember]
           
    public int EmpNo { get; set; }

           
    [DataMember]
           
    public string EmpName { get; set; }
           
    public List<Department> AllDepart { get; set; }
           
    [DataMember]
           
    public string DeptName { get; set; }
           
       
    }

    AllDepartt must have a [DataMember] attribute or it's never goimg to be seen as part of Employee.

    Monday, July 7, 2014 8:19 PM
  • Hi,

    As @darnold924 said, please try to add the [DataMember] to the AllDepart as following:

    [DataMember]
     public List<Department> AllDepart { get; set; }


    Once it has been done, the result will look like below:

    Best Regards,
    Amy Peng


    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, July 8, 2014 2:37 AM
    Moderator
  • Thank you for replying

    I want output like this

    How can i get?

    <ArrayOfEmployee xmlns="http://schemas.datacontract.org/2004/07/WcfrestService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Employee>
    <DeptName>DEV</DeptName>
    <EmpName>Anil</EmpName>
    <Sales>
    <Name>jjj</Name>
    ................
    ...............
    ...............
    </Sales> <AllDepart> <Department> <DeptNo>1</DeptNo> <Dname>Sales</Dname> <Location>Vij</Location> </Department> <Department>...</Department> <Department>...</Department> </AllDepart> <EmpNo>101</EmpNo> </Employee> </ArrayOfEmployee>


    anilbabu




    • Edited by anil33 Wednesday, July 9, 2014 2:38 AM
    Tuesday, July 8, 2014 11:16 AM
  • Hi,
    Please try to modify your code as following:
    In the IService:

    [ServiceContract]
        public interface IService1
        {
            [WebGet(UriTemplate = "Employee", ResponseFormat = WebMessageFormat.Xml)]
            [OperationContract]
            List<Employees> GetAllEmployeeDetails();
    
    
    
            // TODO: Add your service operations here
        }
    
        [DataContract]
        public class Employees
        {
            [DataMember(Order=0)]
            public Employee Employee { get; set; }
            [DataMember(Order=1)]
            public List<Department> AllDepart { get; set; }
    
        }
        
    
        // Use a data contract as illustrated in the sample below to add composite types to service operations.
         [DataContract]
        public class Employee
        {
            [DataMember]
            public int EmpNo { get; set; }
    
            [DataMember]
            public string EmpName { get; set; }
            
            [DataMember]
            public string DeptName { get; set; }
            
        }
    
        [DataContract]
        public class Department
        {
            [DataMember]
            public int DeptNo { get; set; }
    
            [DataMember]
            public string Dname { get; set; }
    
            [DataMember]
            public string Location { get; set; }
            //public List<Employee> Allempl { get; set; }
    
           
        }
        public partial class EmployeeData
        {
            private static readonly EmployeeData _instance = new EmployeeData();
    
            private EmployeeData() { }
    
            public static EmployeeData Instance
            {
                get
                {
                    return _instance;
                }
            }
            List<Employees> empList = new List<Employees>
            {
                    //new Employee() {EmpNo=101,EmpName="Anil",DeptName="DEV"},
                    //new Employee() {EmpNo=102,EmpName="Anu",DeptName="SAL"},
                    //new Employee() {EmpNo=103,EmpName="Teja",DeptName="Mar"},
                    //new Employee() {EmpNo=104,EmpName="Anu",DeptName="SAL"},
                    //new Employee() {EmpNo=105,EmpName="Teja",DeptName="Mar"}
                    new Employees() { Employee=new Employee(){EmpNo=101,EmpName="Anil",DeptName="DEV"}, 
                          AllDepart=new List<Department> { new Department (){ DeptNo=1,Dname="Sales",Location="Vij"}
                                                         
                        }}
                        //AllDepart=new List<Department> { new Department (){ DeptNo=1,Dname="Sales",Location="Vij"},
                        //                                 new Department (){ DeptNo=2,Dname="DEv",Location="Gun"},
                        //                                 new Department (){ DeptNo=3,Dname="pro",Location="Ong"}
                        //}}
            };
            public List<Employees> EmployeeList
            {
                get
                {
                    return empList;
                }
            }
            
        }

    Then in the Service:
      public List<Employees> GetAllEmployeeDetails()
            {
                return EmployeeData.Instance.EmployeeList;
    
            }


    Then result:


    Best Regards,
    Amy Peng

    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.

    Wednesday, July 9, 2014 2:35 AM
    Moderator
  • Hi,

    I see that you have edited your post, then for the edited post, please try to modify your code as following:

         [ServiceContract]
        public interface IService
        {        
            [WebGet(UriTemplate = "Employee", ResponseFormat = WebMessageFormat.Xml)]
            [OperationContract]
            List<Employee> GetAllEmployeeDetails();
    }
        // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
        [DataContract]
        public class Employee
        {
            [DataMember(Order = 4)]
            public int EmpNo { get; set; }
    
            [DataMember(Order = 1)]
            public string EmpName { get; set; }
            [DataMember(Order = 3)]
            public List<Department> AllDepart { get; set; }
            [DataMember(Order=0)]
            public string DeptName { get; set; }
            [DataMember(Order = 2)]
            public Sales Sales { get; set; }
            
        }
    
    
    [DataContract]
        public class Sales
        {
            [DataMember]
            public string Name { get; set; }
    
            [DataMember]
            public string pname { get; set; }
    }
        [DataContract]
    
    
        public class Department
        {
            [DataMember]
            public int DeptNo { get; set; }
    
            [DataMember]
            public string Dname { get; set; }
    
            [DataMember]
            public string Location { get; set; }
            //public List<Employee> Allempl { get; set; }
    
           
        }
        public partial class EmployeeData
        {
            private static readonly EmployeeData _instance = new EmployeeData();
    
            private EmployeeData() { }
    
            public static EmployeeData Instance
            {
                get
                {
                    return _instance;
                }
            }
    
            List<Employee> empList = new List<Employee>
            {
                      new Employee() { EmpNo=101,EmpName="Anil",DeptName="DEV", 
                        AllDepart=new List<Department> { new Department (){ DeptNo=1,Dname="Sales",Location="Vij"},new Department (){ DeptNo=2,Dname="DEv",Location="Gun"},
       new Department (){ DeptNo=3,Dname="pro",Location="Ong"}},
    
                                          Sales=new Sales (){Name="jjjj",pname="ggg"}}
    
                        };
    
    
          
    
    
            public List<Employee> EmployeeList
            {
                get
                {
                    return empList;
                }
            }
            
        }
    Then result:


    Best Regards,
    Amy Peng

    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.

    • Marked as answer by anil33 Friday, July 11, 2014 1:41 PM
    Friday, July 11, 2014 2:58 AM
    Moderator
  • Thank you so much,I have one more thing same way inserting data also in same order

    how to insert data

    public void Add(Employee newEmployee)
            {
                empList.Add(new Employee
                {
                    EmpNo = newEmployee.EmpNo,
                    EmpName = newEmployee.EmpName,
                    DeptName = newEmployee.DeptName,

                });
            }

    public void Update(Employee updEmployee)
            {
                Employee existing = empList.Find(p => p.EmpNo == updEmployee.EmpNo);

                if (existing == null)
                    throw new KeyNotFoundException("Specified Employee cannot be found");

                existing.EmpName = updEmployee.EmpName;
                existing.DeptName = updEmployee.DeptName;
                //existing.Age = updEmployee.Age;
            }

    IService:

    [WebInvoke(Method = "PUT", UriTemplate = "EmployeePUT", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
            [OperationContract]
            void UpdateEmployee(Employee newEmp);

            [WebInvoke(Method = "POST", UriTemplate = "EmployeePOST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
            [OperationContract]
            void AddEmployee(Employee newEmp);

    Service:

    public void UpdateEmployee(Employee newEmp)
            {
                EmployeeData.Instance.Update(newEmp);
            }


            public void AddEmployee(Employee newEmp)
            {
                EmployeeData.Instance.Add(newEmp);
            }


    anilbabu

    Friday, July 11, 2014 11:40 AM
  • If the replies solves your problem, then it will be better if you can mark it as answer. Then it will help the others who meet the same question as you.

    For your last question, could you please try to open a new thread for it(ask the question in a new thread), then the new thread will be easily found by many others WCF experts. And they will help you.


    Friday, July 11, 2014 12:11 PM
  • Ok,

    You know the solution please give me the answer


    anilbabu

    Friday, July 11, 2014 1:54 PM