且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用数据表按日期分组

更新时间:2023-12-01 16:05:22

我们不知道你在做什么,以及你如何试图展示/处理这个问题,我们可以具体。你从中获取数据也可能是相关的:如果你从SQL中检索数据,那么在SQL中进行这种分组!



但原则是非常简单:

按日期分组忽略时间,然后按位置:

这样就可以将记录分成你需要的串。

然后选择日期的MIN和MAX并仅返回时间部分,加上它们与分组字段之间的差异。



但正如我所说:究竟如何这取决于你如何处理事情,我们不知道这些!


 DataTable dt = new DataTable(); 
dt.Columns.AddRange(new DataColumn []
{
new DataColumn(Date,typeof(DateTime)),
new DataColumn(Location,typeof(string) )),
});
dt.Rows.Add(DateTime.ParseExact(23/07/2014 10:30,dd / MM / yyyy HH:mm,新文化信息(fr-fr)),近高速公路);
dt.Rows.Add(DateTime.ParseExact(23/07/2014 11:30,dd / MM / yyyy HH:mm,新文化信息(fr-fr)),近高速公路);
dt.Rows.Add(DateTime.ParseExact(23/07/2014 12:30,dd / MM / yyyy HH:mm,新文化信息(fr-fr)),近高速公路);
dt.Rows.Add(DateTime.ParseExact(24/07/2014 01:00,dd / MM / yyyy HH:mm,new CultureInfo(fr-fr)),来自德克萨斯州);
dt.Rows.Add(DateTime.ParseExact(24/07/2014 01:10,dd / MM / yyyy HH:mm,new CultureInfo(fr-fr)),来自德克萨斯州);
dt.Rows.Add(DateTime.ParseExact(24/07/2014 01:20,dd / MM / yyyy HH:mm,new CultureInfo(fr-fr)),来自德克萨斯州);
dt.Rows.Add(DateTime.ParseExact(24/07/2014 01:30,dd / MM / yyyy HH:mm,new CultureInfo(fr-fr)),来自德克萨斯州);

DataTable dt2 = new DataTable();
dt2.Columns.AddRange(new DataColumn []
{
new DataColumn(Date),
new DataColumn(Start time),
new DataColumn (结束时间),
新DataColumn(持续时间),
新DataColumn(位置)
});
var groups = dt.AsEnumerable()。GroupBy(row => row [Location]);
foreach(var group in groups)
{
Console.WriteLine(group.Key);
var groups2 = group.OrderBy(row => row [Date])。GroupBy(row => Convert.ToDateTime(row [Date])。ToString(dd / MM / yyyy ));
foreach(group2中的var group2)
{
Console.WriteLine(\t+ group2.Key);
var list = group2.ToList();
var startDate = Convert.ToDateTime(list [0] [Date]);
var endDate = Convert.ToDateTime(list [list.Count-1] [Date]);
TimeSpan ts = endDate-startDate;
dt2.Rows.Add(group2.Key,startDate.ToString(HH:mm),endDate.ToString(HH:mm),ts.Hours +hour+ ts.Minutes +minutes ,group.Key);
}
}


Hi friends,

I have a problem while grouping my datatable,

Here is the data,

Date		        Location
23/07/2014 10:30	Near Highway
23/07/2014 11:30	Near Highway
23/07/2014 12:30	Near Highway
24/07/2014 01:00	From Texas 
24/07/2014 01:10	From Texas 
24/07/2014 01:20	From Texas 
24/07/2014 01:30	From Texas 



And I want to group by and manipulate the start time, end time and duration based on the location.

Date     	Start time	End time	Duration	Location
23/07/2014	10:30	        12:30	        2 hour 00 min	Near Highway
24/07/2014	01:00	        01:30	        0 hours30 min	From Texas 



Kindly help me to bring up this solution. Thanks in advance.

Without knowing a lot more about what you are doing, and how you are trying to display / process this, we can;t be specific. And where you get the data from may well be relevant as well: if you are retrieving the data from SQL, then do this grouping in SQL!

But the principle s pretty simple:
Group by date ignoring the time, then by Location:
This gives you the records broken into the bunches you need.
Then select the MIN and MAX of the Date and return the time portion only, plus the difference between them and the grouping fields.

But as I say: Exactly how to do it will depend on how you are handling things, and we don't know any of that!


DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] 
    {
        new DataColumn("Date",typeof(DateTime)),
        new DataColumn("Location",typeof(string)),
    });
    dt.Rows.Add(DateTime.ParseExact("23/07/2014 10:30", "dd/MM/yyyy HH:mm", new CultureInfo("fr-fr")), "Near Highway");
    dt.Rows.Add(DateTime.ParseExact("23/07/2014 11:30", "dd/MM/yyyy HH:mm", new CultureInfo("fr-fr")), "Near Highway");
    dt.Rows.Add(DateTime.ParseExact("23/07/2014 12:30", "dd/MM/yyyy HH:mm", new CultureInfo("fr-fr")), "Near Highway");
    dt.Rows.Add(DateTime.ParseExact("24/07/2014 01:00", "dd/MM/yyyy HH:mm", new CultureInfo("fr-fr")), "From Texas");
    dt.Rows.Add(DateTime.ParseExact("24/07/2014 01:10", "dd/MM/yyyy HH:mm", new CultureInfo("fr-fr")), "From Texas");
    dt.Rows.Add(DateTime.ParseExact("24/07/2014 01:20", "dd/MM/yyyy HH:mm", new CultureInfo("fr-fr")), "From Texas");
    dt.Rows.Add(DateTime.ParseExact("24/07/2014 01:30", "dd/MM/yyyy HH:mm", new CultureInfo("fr-fr")), "From Texas");

    DataTable dt2 = new DataTable();
    dt2.Columns.AddRange(new DataColumn[] 
    {
        new DataColumn("Date"),
        new DataColumn("Start time"),
        new DataColumn("End time"),
        new DataColumn("Duration"),
        new DataColumn("Location")
    });
    var groups = dt.AsEnumerable().GroupBy(row => row["Location"]);
    foreach (var group in groups)
    {
        Console.WriteLine(group.Key);
        var groups2 = group.OrderBy(row => row["Date"]).GroupBy(row => Convert.ToDateTime(row["Date"]).ToString("dd/MM/yyyy"));
        foreach (var group2 in groups2)
        {
            Console.WriteLine("\t" + group2.Key);
            var list = group2.ToList();
            var startDate = Convert.ToDateTime(list[0]["Date"]);
            var endDate = Convert.ToDateTime(list[list.Count-1]["Date"]);
            TimeSpan ts = endDate-startDate;
            dt2.Rows.Add(group2.Key, startDate.ToString("HH:mm"), endDate.ToString("HH:mm"), ts.Hours + " hour " + ts.Minutes + " minutes", group.Key);
        }
    }