我正在尝试计算本周更新的一些记录,并将它们按星期几分组(取决于它们上次更新的时间)。 E.g.So周二:1,Thur:4周五:5等...我不确定如何按周分组。
public JSONObject verifyKey(String key, String phone, MaskerPane mp){
this.mp=mp;
String url = "http://theUrl";
String httpcall = httpcall(url, "func","verify","key",key,"phone",phone,"type","eDesk");
try {
return new JSONObject(httpcall);
} catch (JSONException ex) {
Logger.getLogger(Network.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
public String httpcall(String url,String ...args) {
try {
// String charset = "UTF-8"; // Or in Java 7 and later, use the constant: java.nio.charset.StandardCharsets.UTF_8.name()
String charset = java.nio.charset.StandardCharsets.UTF_8.name();
String formatin = "";
Object values[] = new String[args.length/2];
int valCount =0;
for (int i = 0; i < args.length; i+=2) {
formatin+=args[i]+"=%s&";
values[valCount]=URLEncoder.encode(args[i+1], charset);
valCount++;
}
String query = String.format(formatin,values);
query=query.substring(0, query.length()-1);
//Remember to remove proxy lines before production
//SocketAddress addr = new InetSocketAddress("127.0.0.1", 8080);
//Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
URLConnection connection = new URL(url).openConnection(proxy);
connection.setDoOutput(true); // Triggers POST.
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
try (OutputStream output = connection.getOutputStream()) {
output.write(query.getBytes(charset));
}
InputStream response = connection.getInputStream();
//return getStringFromInputStream(response);
} catch (UnsupportedEncodingException ex) {
if(mp!=null)mp.setVisible(false);
Logger.getLogger(Buy.class.getName()).log(Level.SEVERE, null, ex);
}catch (MalformedURLException ex) {
if(mp!=null)mp.setVisible(false);
Logger.getLogger(Buy.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
//if(mp!=null)mp.setVisible(false);
Logger.getLogger(Buy.class.getName()).log(Level.SEVERE, null, ex);
//return "{\"ans\":\"1\"}";//xpectd on success
//return "{\"ans\":\"0\",\"err\":\"1\"}";//xpectd on failure
}
return null;
}
我已经尝试过.GroupBy(o =&gt; o.LastUpdated.DayOfWeek但是会抛出错误: “LINQ to Entities”不支持指定的类型成员'DayOfWeek'。
答案 0 :(得分:3)
消息是显式的,Entity Framework不知道如何将“DayOfWeek”翻译成SQL。最简单的解决方案是在检索数据后在SQL之外进行分组:
var data = repo
.Where(o => o.LastUpdated >= monday)
.AsEnumerable() // After this everything uses LINQ to Objects and is executed locally, not on your SQL server
.GroupBy(o => o.LastUpdated)
.Select(g => new { DayOfWeek = g.Key, Count = g.Count() })
.ToList();
它不应该对性能产生任何影响,因为你没有进一步向下过滤,所以你没有从服务器检索比你需要的更多的数据,过去AsEnumerable的任何东西都被实现为数据,只是générâtes一个SQL查询之前的任何东西,所以过去AsEnumerable(或任何其他可以实现查询的东西,如ToArray或ToList),你可以使用你在C#中通常使用的任何东西,而不必担心它可以转换为SQL。
答案 1 :(得分:3)
如果您只定位SqlServer数据库类型,则可以使用SqlFunctions.DatePart这样的规范函数
var data = repo
.Where(o => o.LastUpdated >= monday)
.GroupBy(o => SqlFunctions.DatePart("weekday", o.LastUpdated))
.Select(g => new { DayOfWeek = g.Key, Count = g.Count() })
.ToList();
不幸的是,DbFunctions中没有定义这样的通用规范函数,因此如果您要定位另一种数据库类型(或多种数据库类型),唯一的选择是切换到 Linq To Objects another answer中描述的上下文。
答案 2 :(得分:0)
只能使用datetime的lastupdated column数据类型。
var data = repo.Where(o => o.LastUpdated >= monday).AsEnumerable().GroupBy(o => o.LastUpdated.Value.Day).Select(g => new { DayOfWeek = g.Key, Count = g.Count() }).ToList();