© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
39 replies
LukeJ

✅ Cannot convert from anonymous type

I have a method that takes effectively extends OrderBy, and so one of it's paramaters is a keySelector, just like in OrderBy
Func<TElement, TKey> selector
Func<TElement, TKey> selector

In my use case I separated out the intended key to make it read better, so here is it's signature
private static int SelectByDistanceFromEnemy(this (Option<Unit> unit, int index) units)
private static int SelectByDistanceFromEnemy(this (Option<Unit> unit, int index) units)

I'm then using it in this code (lambda to call
SelectByDistanceFromEnemy
SelectByDistanceFromEnemy
just to try make the error easier to parse)
nits.Select((unit, index) => new { unit, index })
  .RunOrderedDescending(tpl => SelectByDistanceFromEnemy(tpl),
  ...
nits.Select((unit, index) => new { unit, index })
  .RunOrderedDescending(tpl => SelectByDistanceFromEnemy(tpl),
  ...

which gives me this error
cannot convert from '<anonymous type: LanguageExt.Option<Unit> unit, int index>' to '(LanguageExt.Option<Unit> unit, int index)
cannot convert from '<anonymous type: LanguageExt.Option<Unit> unit, int index>' to '(LanguageExt.Option<Unit> unit, int index)

I don't get why it can't though. The only difference is one is an anonymous type. Anyone know why?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

cannot convert from 'TypeA' to 'TypeB' with generics
C#CC# / help
2y ago
✅ Cannot implicit convert type
C#CC# / help
3y ago
✅ Anonymous type
C#CC# / help
3y ago