Mapping

  • Thread starter Thread starter shapper
  • Start date Start date
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
 
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.
 
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
 
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.
 
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.
 
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
 
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.
 
Back
Top