您应该可以在CodeFirstStoreFunctions的
Where条件中使用标量SQL函数
假设您要映射SQL函数[dbo]。[LatLongDistanceCalc],并根据测试套件:
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(); }}用法将是:
context.Locations .Where(e => MyDataContext.LatLongDistanceCalc(e.Lat, e.Long, lat, long) >= 10)



