Mapping

S

shapper

Hello,

I am mapping a class Error to another class YError:

YError ToY(Error error) {
return new YError {
Id = error.ErrorId,
CreatedAt = error.CreatedAt,
Description = error.Description,
HttpCode = error.HttpCode,
};
} // ToY

Now I am trying a new version that does the mapping for many errors:

IQueryable<YError> ToY(IQueryable<Error> error) {
return error.Select(e => e = <YError>ToY(error)).AsQueryable();
} // ToY

But I get the error:
Cannot implicitly convert type
'System.Linq.IQueryable<MyApp.Models.YError>' to 'MyApp.Models.Error'.
An explicit conversion exists (are you missing a cast?)

What am I missing?

Thanks,
Miguel
 
A

Anthony Jones

shapper said:
Hello,

I am mapping a class Error to another class YError:

YError ToY(Error error) {
return new YError {
Id = error.ErrorId,
CreatedAt = error.CreatedAt,
Description = error.Description,
HttpCode = error.HttpCode,
};
} // ToY

Now I am trying a new version that does the mapping for many errors:

IQueryable<YError> ToY(IQueryable<Error> error) {
return error.Select(e => e = <YError>ToY(error)).AsQueryable();
} // ToY

But I get the error:
Cannot implicitly convert type
'System.Linq.IQueryable<MyApp.Models.YError>' to 'MyApp.Models.Error'.
An explicit conversion exists (are you missing a cast?)

What am I missing?

IQueryable<YError> ToYs(IQueryable<Error> error) {
return error.Select(e => ToY(e)).AsQueryable();
}

Note because the method returns a different type than ToY you should not use
the same method name.
 
S

shapper

IQueryable<YError> ToYs(IQueryable<Error> error) {
  return error.Select(e => ToY(e)).AsQueryable();

}

Note because the method returns a different type than ToY you should not use
the same method name.

I though that because the inputs were of different types that I could
name the methods the same since when I was typing I even got the two
options.

Thanks,
Miguel
 
A

Anthony Jones

IQueryable<YError> ToYs(IQueryable<Error> error) {
return error.Select(e => ToY(e)).AsQueryable();

}

Note because the method returns a different type than ToY you should not
use
the same method name.
I though that because the inputs were of different types that I could
name the methods the same since when I was typing I even got the two
options.
<<<<<<<<<<<<

using overloads to create methods that have the same name but take different
sets of parameters is fine and very useful.

C# allows you to return a different type per overload but to make use of
that facility is not a good idea. Its much better for everyones sanity that
whilst a method may a various overloads it returns the same type.

The only case where I think this may be barely acceptable is where a deeper
set of parameters semantically implies a greater specialisation of base
class than a simpler overload. In which case it may be acceptable that the
return value be a derived class of the type returned in the simpler
overloads. In this way even if the LHS has the base class type nothing bad
happens.
 
S

shapper

I though that because the inputs were of different types that I could
name the methods the same since when I was typing I even got the two
options.
<<<<<<<<<<<<

using overloads to create methods that have the same name but take different
sets of parameters is fine and very useful.

C#  allows you to return a different type per overload but to make use of
that facility is not a good idea.  Its much better for everyones sanitythat
whilst a method may a various overloads it returns the same type.

The only case where I think this may be barely acceptable is where a deeper
set of parameters semantically implies a greater specialisation of base
class than a simpler overload.  In which case it may be acceptable thatthe
return value be a derived class of the type returned in the simpler
overloads.  In this way even if the LHS has the base class type nothingbad
happens.

Thank you Anthony for the advice.
 
F

Family Tree Mike

shapper said:
Hello,

I am mapping a class Error to another class YError:

YError ToY(Error error) {
return new YError {
Id = error.ErrorId,
CreatedAt = error.CreatedAt,
Description = error.Description,
HttpCode = error.HttpCode,
};
} // ToY

Now I am trying a new version that does the mapping for many errors:

IQueryable<YError> ToY(IQueryable<Error> error) {
return error.Select(e => e = <YError>ToY(error)).AsQueryable();
} // ToY

But I get the error:
Cannot implicitly convert type
'System.Linq.IQueryable<MyApp.Models.YError>' to 'MyApp.Models.Error'.
An explicit conversion exists (are you missing a cast?)

What am I missing?

Thanks,
Miguel

Doesn't this work to do what you want?

IQueryable<YError> ToY ( IQueryable<Error> error )
{
return error.Select ( e => ToY ( e ) );
} // ToY


Mike
 
S

shapper

Doesn't this work to do what you want?

IQueryable<YError> ToY ( IQueryable<Error> error )
{
    return error.Select ( e =>  ToY ( e ) );

} // ToY

Mike

Yes, it works. I was just saying thanks for the advice of no using
ToYs instead of ToY.

I understand that it makes easier to understand the code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Casting 1
Conversione between Hashtable and IDictionary 2
Round 3
generic interface question 1
enum with underlying type 1
using enums across a web app 1
implicit cast operator funny-ness 10
Create user 1

Top