aman
aman

Reputation: 6242

Cast list from one type to another

I am having issues in passing list to a method that accepts parameter of different type.

I have the following method in my namespace: Common.DB2 (One classlibrary)

public List<MyModel> GetDB2Data()
{
 //Get data from db2
}

Where MyModel belongs to Common.Db2 namespace

I have another method in my namespace Common.Sql (Another Classlibrary)

public void PostToSql(List<MyModel> listData)
{
 //Post data to sql
}

Where MyModel belongs to Common.Sql namespace

So basically I get some data from my db2 and post it to sql.

In another class library I get this data and post data as:

//Get Data
var data =  db2Repo.GetDB2Data();

//Post Data 
sqlRepo.PostToSql(data);

Here in the above line it fails stating

Argument 1: cannot convert from 
  'System.Collections.Generic.List<Common.DB2.MyModel>' to 
  'System.Collections.Generic.List<Common.Sql.MyModel>'

I had tried casting like below:

List<Common.Sql.MyModel> sourceList = new List<Common.Sql.MyModel>();
sourceList =  data.Cast<Common.Sql.MyModel>().ToList();

But above again throws exception

Unable to cast object of type 'Common.DB2.MyModel' to type 
'Common.Sql.MyModel'.

Any better way to handle/design this.

Upvotes: 0

Views: 885

Answers (4)

Bruno Avelar
Bruno Avelar

Reputation: 670

If you want to use the .Cast method, you need to implement the casting operators. You can check how to do it here. How do I provide custom cast support for my class?

Or you can do this using the .Select method, but you'll need to initialize each object by yourself.

//Get Data
var data =  db2Repo.GetDB2Data();

var sqlData = data.Select(x => 
    new Common.Sql.MyModel(){ 
    // Do your mapping here 
    });

//Post Data 
sqlRepo.PostToSql(sqlData );

My suggestion for this scenario: If these models are part of your domain, create the mapping in whenever your architecture tells you to do. Otherwise, just create constructors to do the mapping.

You can learn more about .Select here: https://msdn.microsoft.com/en-us/library/bb548891(v=vs.110).aspx

Upvotes: 2

Crispy Holiday
Crispy Holiday

Reputation: 472

Casting between two models won't work just like that. Your code doesn't know how to map those two models to each other.

You can use some mapping package like AutoMapper for that, or just create a method that converts between the two in some "helper" or whatever.

Upvotes: 3

Phil S
Phil S

Reputation: 846

Is the same object in both namespaces? If so, you should delete one copy and simply reference the namespace in your other class.

If they're NOT the same model, you'll need to manually perform some sort of cast - i.e. make a new copy of your destination model and then map it: SQL.MyModel.ID = DB2.MyModel.ID etc

Upvotes: 0

Joey
Joey

Reputation: 354516

Well, they're different types, so it's hard to fault the runtime for not doing what you want, since it cannot be done.

If both types are more or less the same, then you could write conversion methods between them and use them to convert the collections:

var sourceList = data.Select(Conversions.Db2ToSqlModel).ToList();

If you can modify the types, you could also provide conversion operators, but the end result is pretty much the same.

Upvotes: 0

Related Questions