2013-12-08 31 views
0

我正在研究地圖應用程序,需要能夠將位置值的巨大數據集削減爲更合理的值。所以我想要做的是什麼組的位置由四捨五入值,如下面的SQL:實體框架/ LINQ回合和計數緯度/經度

select ROUND(Latitude, 0), ROUND(Longitude, 0), count(*) from tweet 
group by ROUND(Latitude, 0), ROUND(Longitude, 0) 
order by count(*) desc 

我永遠無法分組工作,我需要它雖與EF和/或LINQ(主要是因爲該方法我似乎無法理解它是如何工作的)。理想情況下,我希望在EF查詢中擁有它,以便它成爲SQL語句的一部分,並且在執行一些LINQ操作之前,我不必將巨型數據集放入List中;但是如果它必須做到這一點,那麼就這樣吧。

我能做到這樣,但我不想:

var items = context.ExecuteStoreQuery<DataPoint>("select ROUND(Latitude, 0) as lat, ROUND(Longitude, 0)as lon, count(*) as weight from location " + 
"group by ROUND(Latitude, 0), ROUND(Longitude, 0) " + 
"order by count(*) desc", null).ToList(); 

編輯: 最終的解決方案(有位來回)低於可接受的最終看上去像這樣:

private class DataPoint 
{ 
    public double lat { get; set; } 
    public double lon { get; set; } 
    public int weight { get; set; } 
} 

var mydata = (from t in locations 
group t by new { 
Latitude = EntityFunctions.Truncate(t.Latitude, accuracy).Value, 
Longitude = EntityFunctions.Truncate(t.Longitude, accuracy).Value } 
into g 
orderby g.Count() 
select new DataPoint{ lat = g.Key.Latitude, lon = g.Key.Longitude, weight = g.Count() }).ToList(); 

不要忘了添加此using語句:

using System.Data.Objects; 

回答

0
from t in context.Tweets 
group t by new { t.Latitude, t.Longitude } into g 
order by g.Count() desc 
select new { g.Key.Latitude, g.Key.Longitude, Count = g.Count() } 
+0

這很接近,但沒有考慮舍入,這是我的主要目標。類似這樣的東西會很接近,但會產生一個運行時錯誤:'group t by new {Latitude = Convert.ToDouble(t.Latitude.ToString(「##。#」)),Longitude = Convert.ToDouble(t.Longitude。 ToString(「##。#」))} into g' – FirstDivision

+0

特別是這個錯誤'LINQ to Entities does not recognized the method'Double ToDouble(System.String)'method,this method can not be translated into a store expression.' – FirstDivision

+0

@FirstDivision:使用http://msdn.microsoft.com/en-us/library/dd412702 – SLaks