1

So, I have a C# project in which, I am loading a XML document (contains name of students and Id) using Linq to xml and I have to get the associated data (their due date, amount and stuff ) from a WCF service. I added the service with just right click and add service reference and now need to pass arrays to the GetData function, which I initialized but its null obviously. I cant able to convert my array to service type and the function returns array too. How do I assign the array to studentArray ?

 ServiceReference1.ServiceClient client = new ServiceReference1.RycorServiceClient();

Application.ServiceReference1.Student[] studentArray = new ServiceReference1.Student[9];

        Student[] array = studentList.ToArray();

        //for (int i = 0; i <= array.Count(); i++)
        //    studentArray[i] = (RycorApplication.ServiceReference1.Student)array[i];

        //this gives me an error says Cannot convert type 'Application.Student' to 'Application.ServiceReference1.Student'.

        var data = client.GetData(studentArray);

After getting this data, how do I save this data to my XML file ?

2 Answers 2

1

You are getting this error because Application.Student is a different type, you can try to use Application.ServiceReference.Student to save the list of students instead of the "studentList" type.

I suppose that "studentList is an "Application.Student" list and you have to use the same model or make a copy between them using something like this (in the first answer): Copy values from one object to another

Sign up to request clarification or add additional context in comments.

5 Comments

List<Student> studentList is a list of values i loaded from the xml file, in the Student class I created.
studentArray is empty, i need to pass array of students in getdata funtion in order to get data but student Array is empty rightnow
The problem is not the content (it's empty). The problem is its type. Try to do it with Application.ServiceReference1.Student[] array = new ServiceReference1.Student[9];
thats what i am doing, array is Application.ServiceRefrence1.Student[] type, same as studentArray in my code but what type should be array type in my code.
No it isn't. Look at array declaration: Student[] array = studentList.ToArray(); This is a different type than Application.ServiceReference1.Student[].
0

You pretty much have to do this:

List<ServiceReference1.Student> wcfStudentList = new System.Collections.Generic.List<ServiceReference1.Student>();
        foreach (var student in studentList)
        {
            wcfStudentList.Add(new ServiceReference1.Student()
            {
                ID = student.ID,
                Name = student.Name,
                ..etc..
            });
        }
        var data = client.GetStudentData(wcfStudentList.ToArray());

I do have to question why you don't just change the WCF call if you can to take a List of the student IDs instead of passing the entire object though?

3 Comments

because, the project i doing, they already gave the service, i did not created the wcf service.
studentArray is Empty, its just empty array of 9.
Edited for less confusion (hopefully). Basically, the only way I know here is to iterate over the array of students that you already have and convert each object into the type that the wcf service is expecting.