How to use scalar-valued function with linq to entity? [duplicate] How to use scalar-valued function with linq to entity? [duplicate] sql sql

How to use scalar-valued function with linq to entity? [duplicate]


Here is how you do it:

Step 1: In edmx

      <Function Name="DistanceBetween" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">      <CommandText>        select dbo.DistanceBetween(@lat1,@long1,@lat2,@long2)      </CommandText>      <Parameter Name="Lat1" Type="real" Mode="In" />      <Parameter Name="Long1" Type="real" Mode="In" />      <Parameter Name="Lat2" Type="real" Mode="In" />      <Parameter Name="Long2" Type="real" Mode="In" />    </Function>

step 2: Import the function

  1. double click the edmx
  2. In the Model Browser view, expand GeoDataModel.Store (could be named different)
  3. expand stored procedures /function
  4. double click DistanceBetween
  5. Scalars = Single
  6. Click OK

Step 3: In C#:

    GeoDataEntities db = new GeoDataEntities();    var first = db.DistanceBetween(234, 2342, 424, 243).First().Value;

note that IsComposable="false" and no ReturnType and dont forget to add the :

      <CommandText>        select dbo.DistanceBetween(@lat1,@long1,@lat2,@long2)      </CommandText>

Hope that help....