0

I get this error

Error Cannot convert type TestMVC.GetServiceRequests.serviceRequests[] to System.Collections.Generic.IEnumerable<TestMVC.Models.ServiceRequest>

I'm trying to copy .net object array from webservice into another .net c# object array and they both have the same structure.

I tried type casting but this did not work.

var serviceRequest = svcClient.GetServiceRequests(getServiceRequest).responseData;
return  (IEnumerable<ServiceRequest>)  serviceRequests;




ServiceRequest[] serviceRequests = new ServiceRequest[]
{
     new ServiceRequest {  customerName = "Customer123", contactName = "Pat Nelson", vin = "4DRBUAFN75A977722", unitNo = "275", serviceRequestId = "1", requestedDate = "08/08/15", requestedTime = "PM", connectCompleted = "true",
                                  captureCompleted = "false", collectCompleted = "false", agreeCompleted = "false", updateDate = "08/17/2015", createDate = "08/17/2015", note = "note note note note note note note note note note note note note note note note note note note note note note  note note note note ", userId = "yyy1ri3"}, 

     new ServiceRequest {  customerName = "Customer456", contactName = "Pat Nelson", vin = "1HSMTAAN1DH328986", unitNo = "325", serviceRequestId = "2", requestedDate = "09/01/15", requestedTime = "AM", connectCompleted = "false",
                                  captureCompleted = "true", collectCompleted = "true", agreeCompleted = "false", updateDate = "08/17/2015", createDate = "08/17/2015", note = "note note note note note note note note note note note note note note note note note note note note note note  note note note note ", userId = "yyy1ri3"},

};
4
  • 4
    Please post actual code. Commented Apr 3, 2016 at 2:20
  • What kind of Webservice do you use? WCF or something else? Commented Apr 3, 2016 at 2:32
  • It seems pretty clear when you read the error message, a serviceRequests is not a ServiceRequest so an array of serviceRequests shouldn't be considered an IEnumerable of ServiceRequest... unless you can show us the definition of the serviceRequests class and show us that it is. Commented Apr 3, 2016 at 2:38
  • The second set of code you added. is that client side or server/service side? Commented Apr 3, 2016 at 2:55

3 Answers 3

1

When you import a web service as a reference in Visual Studio, new classes are automatically created for the objects returned from calls to the web service.

In this case the GetServiceRequests() methods returns a list of objects of class serviceRequests. This class was automatically created by Visual Studio.

You cannot assign objects of that class to your own class ServiceRequest, even when the classes are defined to have the exact same properties.

So you need to work with the class created by Visual Studio not your own class.

If the web service is a WCF or ASMX webservice, you can tell Visual Studio not to create own classes for the objects returned by the web service. In order for this to work, the commonly used class (in this case ServiceRequest) must be declared in a separate assembly that is referenced by both the webservice and the client calling the webservice. Then when you add the webservice reference to the client, open the "Advanced" dialog and enable "Reuse types in referenced assemblies".

If it is some other type of web service, this is not possible.

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

Comments

1

The error indicates (correctly) that an array of one type cannot be cast as an IEnumerable of a different type.

One is TestMVC.GetServiceRequests.serviceRequests and the other is TestMVC.Models.ServiceRequest.

It doesn't matter whether they have the same structure. One type won't be automatically converted to another.

Don't think of it as an error - the compiler is preventing an error.

Casting a class as another class or interface only works when the class is derived from that class or inherits that interface.

If you have an instance of class A (or an array) and you want class B, and both conveniently have the same properties, then you would need create a new instance of B and copy its properties from A. (In other words, mapping.)

Comments

0

The two types

TestMVC.GetServiceRequests.serviceRequests[]

and

System.Collections.Generic.IEnumerable<TestMVC.Models.ServiceRequest>

do not match.

Given that they have the same structure. You can map the two types in order to get the resulting type you want. Then convert the resulting collection to an array using the ToArray extension method.

This is a manual way to do.

public ServiceRequest[] SomeMethod() {
    //..other code
    TestMVC.GetServiceRequests.serviceRequests[] serviceRequests = svcClient.GetServiceRequests(getServiceRequest).responseData;

    return serviceRequests.Select(s=> new ServiceRequest {
        customerName = s.customerName,
        contactName = s.contactName,
        //...etc...
    }).ToArray();
}

there are mapping frameworks you can also use to do the heavy lifting for you if you object has a lot of properties. One such tool is TinyMapper

which could be used like

public ServiceRequest[] SomeMethod() {
    //..other code
    TestMVC.GetServiceRequests.serviceRequests[] serviceRequests = svcClient.GetServiceRequests(getServiceRequest).responseData;

    return serviceRequests.Select(s=> TinyMapper.Map<ServiceRequest>(s)).ToArray();
}

2 Comments

System.InvalidCastException was unhandled by user code HResult=-2147467262 Message=Unable to cast object of type 'System.Collections.Generic.List1[TestMVC.GetServiceRequests.serviceRequests]' to type 'System.Collections.Generic.IEnumerable1[TestMVC.Models.ServiceRequest]'.
The formatting of the question didn't show TestMVC.Models.ServiceRequest so my original answer was based on that. I've updated the answer given then new information

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.