LINQ Query using UDF that receives parameters from the query
Posted
by
Ben Fidge
on Stack Overflow
See other posts from Stack Overflow
or by Ben Fidge
Published on 2012-09-25T09:36:30Z
Indexed on
2012/09/25
9:37 UTC
Read the original article
Hit count: 315
I need help using a UDF in a LINQ which calculates a users position from a fixed point.
int pointX = 567, int pointY = 534; // random points on a square grid
var q = from n in _context.Users
join m in _context.GetUserDistance(n.posY, n.posY, pointX, pointY, n.UserId) on n.UserId equals m.UserId
select new User() {
PosX = n.PosX,
PosY = n.PosY,
Distance = m.Distance,
Name = n.Name,
UserId = n.UserId
};
The GetUserDistance is just a UDF that returns a single row in a TVP with that users distance from the points deisgnated in pointX and pointY variables, and the designer generates the following for it:
[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.GetUserDistance", IsComposable=true)]
public IQueryable<GetUserDistanceResult> GetUserDistance([global::System.Data.Linq.Mapping.ParameterAttribute(Name="X1", DbType="Int")] System.Nullable<int> x1, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="X2", DbType="Int")] System.Nullable<int> x2, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="Y1", DbType="Int")] System.Nullable<int> y1, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="Y2", DbType="Int")] System.Nullable<int> y2, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="UserId", DbType="Int")] System.Nullable<int> userId)
{
return this.CreateMethodCallQuery<GetUserDistanceResult>(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), x1, x2, y1, y2, userId);
}
when i try to compile i get
The name 'n' does not exist in the current context
© Stack Overflow or respective owner