Home All Groups Group Topic Archive Search About
Author
19 Dec 2008 1:54 PM
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

Author
19 Dec 2008 2:35 PM
Anthony Jones
Show quote Hide quote
"shapper" <mdmo***@gmail.com> wrote in message
news:fc202663-205d-49b5-8928-f46591dfdac4@u18g2000pro.googlegroups.com...
> 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.

--
Anthony Jones - MVP ASP/ASP.NET
Are all your drivers up to date? click for free checkup

Author
19 Dec 2008 2:47 PM
shapper
Show quote Hide quote
On Dec 19, 2:35 pm, "Anthony Jones" <AnthonyWJo***@yadayadayada.com>
wrote:
> "shapper" <mdmo***@gmail.com> wrote in message
>
> news:fc202663-205d-49b5-8928-f46591dfdac4@u18g2000pro.googlegroups.com...
>
>
>
> > 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.
>
> --
> Anthony Jones - MVP ASP/ASP.NET

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
Author
19 Dec 2008 3:15 PM
Anthony Jones
Show quote Hide quote
"shapper" <mdmo***@gmail.com> wrote in message
news:1fab5f84-8f7d-4fac-9784-1c5cc74b9f31@i24g2000prf.googlegroups.com...
On Dec 19, 2:35 pm, "Anthony Jones" <AnthonyWJo***@yadayadayada.com>
wrote:
> "shapper" <mdmo***@gmail.com> wrote in message
>
> news:fc202663-205d-49b5-8928-f46591dfdac4@u18g2000pro.googlegroups.com...
>
>
>
> > 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.
>
>>>>>>>>>>>>
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.

--
Anthony Jones - MVP ASP/ASP.NET
Author
19 Dec 2008 3:32 PM
shapper
Show quote Hide quote
On Dec 19, 3:15 pm, "Anthony Jones" <AnthonyWJo***@yadayadayada.com>
wrote:
> "shapper" <mdmo***@gmail.com> wrote in message
>
> news:1fab5f84-8f7d-4fac-9784-1c5cc74b9f31@i24g2000prf.googlegroups.com...
> On Dec 19, 2:35 pm, "Anthony Jones" <AnthonyWJo***@yadayadayada.com>
> wrote:
>
> > "shapper" <mdmo***@gmail.com> wrote in message
>
> >news:fc202663-205d-49b5-8928-f46591dfdac4@u18g2000pro.googlegroups.com....
>
> > > 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.
>
> 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.
>
> --
> Anthony Jones - MVP ASP/ASP.NET

Thank you Anthony for the advice.
Author
19 Dec 2008 7:03 PM
Family Tree Mike
Show quote Hide quote
"shapper" wrote:

> 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
Author
19 Dec 2008 7:14 PM
shapper
On Dec 19, 7:03 pm, Family Tree Mike
<FamilyTreeM***@discussions.microsoft.com> wrote:
Show quoteHide quote
> "shapper" wrote:
> > 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

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.

Bookmark and Share