|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
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
Show quote
Hide quote
"shapper" <mdmo***@gmail.com> wrote in message IQueryable<YError> ToYs(IQueryable<Error> error) {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? > 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
Show quote
Hide quote
On Dec 19, 2:35 pm, "Anthony Jones" <AnthonyWJo***@yadayadayada.com> I though that because the inputs were of different types that I couldwrote: > "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 name the methods the same since when I was typing I even got the two options. Thanks, Miguel
Show quote
Hide quote
"shapper" <mdmo***@gmail.com> wrote in message I though that because the inputs were of different types that I couldnews: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. > >>>>>>>>>>>> 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
Show quote
Hide quote
On Dec 19, 3:15 pm, "Anthony Jones" <AnthonyWJo***@yadayadayada.com> Thank you Anthony for the advice.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
Show quote
Hide quote
"shapper" wrote: Doesn't this work to do what you want?> 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 > > IQueryable<YError> ToY ( IQueryable<Error> error ) { return error.Select ( e => ToY ( e ) ); } // ToY Mike On Dec 19, 7:03 pm, Family Tree Mike
<FamilyTreeM***@discussions.microsoft.com> wrote: Show quoteHide quote > "shapper" wrote: Yes, it works. I was just saying thanks for the advice of no using> > 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 ToYs instead of ToY. I understand that it makes easier to understand the code.
Other interesting topics
Inheritance problem
Inheritance - Accessing two levels above mine - Now with virtual+override About generic List<string> Finding a specific element using LINQ Help with RegEx how to use count on a collection Inheritance - Accessing two levels above mine Data Objects vs fields relation between cryptoAPI generated machinekeystore and the key p Hashtable memory usage |
|||||||||||||||||||||||