5

This fails with an error:

private IQueryable<Field> PrepareAllFieldsQuery( ref DbGeography geo, int page, int amount, string sort, string order, ISearchCriteria searchCriteria )
{
    var query = this.context.Fields
                            .Where( x => x.DeletedAt == null )
                            .OrderBy( x => x.GeoLocation.Distance( geo ) );
    ...
}

This runs fine

private IQueryable<Field> PrepareAllFieldsQuery( DbGeography geo, int page, int amount, string sort, string order, ISearchCriteria searchCriteria )
{
    var query = this.context.Fields
                            .Where( x => x.DeletedAt == null )
                            .OrderBy( x => x.GeoLocation.Distance( geo ) );
    ...
}

The difference is my DbGeography is not passed by ref this time.

Any reasons why the .OrderBy( x => x.GeoLocation.Distance( geo ) ); function would throw the following error:

Cannot convert lambda expression to type 'string' because it is not a delegate type

Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
  • I can't even compile it... *Cannot use ref or out parameter 'geo' inside an anonymous method, lambda expression, or query expression* – xanatos Jun 29 '15 at 10:06
  • 1
    This might [post](http://stackoverflow.com/questions/1365689/cannot-use-ref-or-out-parameter-in-lambda-expressions) seems to have the explanation. – rdoubleui Jun 29 '15 at 10:08
  • @rdoubleui - This makes a lot of sense. Thank you man. – Jimmyt1988 Jun 29 '15 at 10:16

1 Answers1

4

The error you get is caused by funny things the overload resolution of C# is doing... It is trying to resolve your OrderBy to the Dynamic Linq "version" of OrderBy, that accepts a string as a parameter.

In general, a more correct error, and the one you get if you remove the using System.Linq.Dynamic; or if you rewrite the statement to force using Queryable.OrderBy, like:

var query = Queryable.OrderBy( this.context.Fields.Where( x => x.DeletedAt == null ),
                               x => x.GeoLocation.Distance( geo ) );

would be Cannot use ref or out parameter 'geo' inside an anonymous method, lambda expression, or query expression. As pointed by rdoubleui, here there is an explanation for that.

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280