user2180842
user2180842

Reputation: 27

Convert C# .net object array into another

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"},

};

Upvotes: 0

Views: 140

Answers (3)

Scott Hannen
Scott Hannen

Reputation: 29262

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.)

Upvotes: 1

Nkosi
Nkosi

Reputation: 247451

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

Upvotes: 0

NineBerry
NineBerry

Reputation: 28509

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.

Upvotes: 1

Related Questions