Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
please comvert this code to linq. i have a problem with this in my linq code

SELECT top 15 * FROM (
    SELECT *, 
        (
            (
                (
                    acos(
                        sin((15.391861273433282 * pi() / 180))
                        *
                        sin(( X * pi() / 180)) + cos((15.391861273433282 * pi() /180 ))
                        *
                        cos(( X * pi() / 180)) * cos(((121.05844856778678 - y) * pi()/180)))
                ) * 180/pi()
            ) * 60 * 1.1515 * 1.609344
        )
    as distance FROM table
) table
WHERE distance <= 2


What I have tried:

please comvert this code to linq. i have a problem with this in my linq code
Posted
Updated 8-Jun-21 22:35pm
v3
Comments
Maciej Los 9-Jun-21 3:39am    
Why?
Member 12960626 9-Jun-21 3:40am    
i want to integrate a full linq to sql coded.

i run that code in sql server to get the location within the specific radius. it run properly. but i cant do it in c# behind the code. im using Linq
CHill60 9-Jun-21 4:04am    
Fair question :laugh:
CHill60 9-Jun-21 4:04am    
What have you tried in Linq and what was the problem?
Member 12960626 9-Jun-21 4:08am    
DataContext phil= new DataContext();
var ss= (from x in phil.table
select new
{
distance = ((SqlFunctions.Acos(SqlFunctions.Sin((15.391861273433282 * SqlFunctions.Pi() / 180))
*
SqlFunctions.Sin((Convert.ToDouble(x.X) * SqlFunctions.Pi() / 180)) + SqlFunctions.Cos((15.391861273433282 * SqlFunctions.Pi() / 180))
*
SqlFunctions.Cos((Convert.ToDouble(x.X) * SqlFunctions.Pi() / 180)) + SqlFunctions.Cos(((121.05844856778678 - Convert.ToDouble(x.y))))
) * 180 / SqlFunctions.Pi()
) * 60 * 1.1515 * 1.609344
)
}).AsQueryable();

var getcoor = from p in ss
where p.distance <= 20
select p;

Response.Write(getcoor.ToArray());


the error is
This function can only be invoked from LINQ to Entities.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NotSupportedException: This function can only be invoked from LINQ to Entities

1 solution

I'm guessing...

Try this:
C#
using (DataContext phil= new DataContext())
{
    var ss= phil.table
        .ToList() //obligatory
        .Select(p => ((Math.Acos(
				Math.Sin((15.391861273433282 * Math.PI / 180.0))
		 		* Math.Sin(( p.X * Math.PI / 180.0)) + Math.Cos((15.391861273433282 * Math.PI /180.0 ))
	            * Math.Cos(( p.X * Math.PI / 180.0)) * Math.Cos(((121.05844856778678 - p.Y) * Math.PI/180.0)))
				* 180.0/Math.PI) * 60.0 * 1.1515 * 1.609344)
	    )
        .Where(d => d<20)
	    .ToList();

    return ss;
}


More at: Querying data via the DbSet | Learn Entity Framework Core[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900