I have a particular LINQ query that is doing a group by an anonymous type and then selecting into an object via projection. The query works fine when selecting in to the object. I am now trying to change the usage of the query to go in to a DTO that contains all the properties being projected to the object, however when I switch the types from the Object to the DTO, the query starts giving me an error that the IQueryable.Select has invalid arguments.
IQueryable<type> result = from o in blah where some_conditions
orderby o.Name
group o by new {o.Value1, o.Value2, o.Value3} into groupedO
select new type
{
Value1=groupedO.Key.value1,
Value2=groupedO.Key.value2,
Value3=groupedO.Key.value3
}
Works fine when “type” is my original object but fails when I make it my DTO that has properties Value1,Value2 and Value3 (of the appropriate types).
I realize this probably isn’t enough information to find the problem, but I’d also accept any pointers as to a direction to look for what could cause the issue. I can’t tell what could possibly be different. Also, if I remove the group, the query works fine, so it is something specific to the anonymous type in the group.
The exact error is
System.Linq.IQueryable<System.Linq.IGrouping<AnonymousType#1,someDalClass>>' does not contain a definition for 'Select' and the best extension method overload 'System.Linq.Enumerable.Select<TSource,TResult>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,TResult>)' has some invalid arguments.
Facepalm moment. The issue was that the DTO had a typo in a field name and wasn’t compiling, however it was lost in the error log. Fixing the typo in the DTO made the types match properly and all is now well. Thanks.