score:32

Accepted answer

you should be able to use a scalar sql function in your where criterias with codefirststorefunctions

assuming you want to map sql function [dbo].[latlongdistancecalc], and according to the test suite:

public class mydatacontext: dbcontext
{
    protected override void onmodelcreating(dbmodelbuilder modelbuilder)
    {
       //...

       modelbuilder.conventions.add(new functionsconvention("dbo", this.gettype()));
    }

    // "codefirstdatabaseschema" is a convention mandatory schema name
    // "latlongdistancecalc" is the name of your function

    [dbfunction("codefirstdatabaseschema", "latlongdistancecalc")]
    public static int latlongdistancecalc(int fromlat, int fromlong,
                                                       int tolat, int tolong)
    {
       // no need to provide an implementation
       throw new notsupportedexception();
    }
}

usage would then be:

context.locations
       .where(e => mydatacontext.latlongdistancecalc(e.lat, e.long, lat, long) >= 10)

Related Query