此代码对序号为12的派对名称进行分组
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class GroupVsPaneDemo extends Application {
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
Group group = new Group();
VBox.setVgrow(group, Priority.NEVER);
VBox.setVgrow(pane, Priority.NEVER);
VBox vbox = new VBox(group, pane);
Rectangle rect1 = new Rectangle(100, 100, 100, 100);
Rectangle rect2 = new Rectangle(100, 100, 100, 100);
Rectangle rect3 = new Rectangle(200, 200, 100, 100);
Rectangle rect4 = new Rectangle(200, 200, 100, 100);
rect1.setFill(Color.BLUE);
rect2.setFill(Color.BLUE);
rect3.setFill(Color.GREEN);
rect4.setFill(Color.GREEN);
group.getChildren().addAll(rect1, rect3);
pane.getChildren().addAll(rect2, rect4);
Scene scene = new Scene(vbox, 800, 800);
scene.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
double deltaX ;
switch(e.getCode()) {
case LEFT:
deltaX = -10 ;
break ;
case RIGHT:
deltaX = 10 ;
break ;
default:
deltaX = 0 ;
}
rect3.setX(rect3.getX() + deltaX);
rect4.setX(rect4.getX() + deltaX);
});
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
现在我想知道如何在多个字段上应用组,其序号位置为0表示group line by line[12] into newGroup
line[0]
将包含日期和时间。我只想按时分组。这样我就可以提取时间部分
line[0]
1)只是帮我分组多个字段一个是行[12]而另一个是行[0]
2)DateTime.Parse(line[0].Substring(line[0].Length-8))
var csvlines = File.ReadAllLines(@"M:\smdr(backup08-06-2015).csv");
var csvLinesData = csvlines.Skip(1).Select(l => l.Split(',').ToArray());
var groupOfUser = from line in csvLinesData
group line by line[12] into newGroup
orderby newGroup.Key
select newGroup;
var user = (from userOfGrp in groupOfUser
select
new User()
{
CSRName = userOfGrp.Key,
Incomming = userOfGrp.Count(x => x[4] == "I"),
Outgoing = userOfGrp.Count(x => x[4] == "O")
}).ToList();
将具有日期和时间部分,如“2015/06/08 07:59:12”,我必须仅在line[0]
<上的时间部分应用组/ p>
此行line[0]
投掷错误无法将类型'string'转换为'System.DateTime'
group r by new { prop1 = r[12], ((DateTime)r[0]).TimeOfDay } into g
答案 0 :(得分:2)
如果我理解你,你需要这个:
from line in csvLinesData
group line by new
{
prop1 = line[12],
Time = DateTime.Parse(line[0]).TimeOfDay
} into newGroup
orderby newGroup.Key.prop1, newGroup.Key.Time
select newGroup;
由于line[0]
是一个字符串(显然),因此您必须将其解析为DateTime
值。我展示了最简单的方法,但也许你应该建立一些更安全的因为不是每个字符串都可以解析为DateTime
。但这超出了这个问题的范围。
请注意,匿名类型具有命名属性,您可能希望重命名prop1
。