score:18
Accepted answer
If your column type is DateTime
then you can group by its Hour
property and get count for each hour like:
var query = dt.AsEnumerable()
.GroupBy(row => row.Field<DateTime>("dateAndTime").Hour)
.Select(grp => new
{
Hour = grp.Key,
Count = grp.Count()
});
where dt
is your DataTable
EDIT: The above should work as long as you have data for a single date in your DataTable
, but if you have rows for multiple dates then grouping should be done on Date
as well as Hour
like:
var query = dt.AsEnumerable()
.GroupBy(row => new
{
Date = row.Field<DateTime>("dateAndTime").Date,
Hour = row.Field<DateTime>("dateAndTime").Hour
})
.Select(grp => new
{
Date = grp.Key.Date,
Hour = grp.Key.Hour,
Count = grp.Count()
});
score:4
I assume that the column really stores DateTime
s, you can group by Date
+ Hour
:
var hourGroups = dataTable.AsEnumerable()
.Select(row => new { DateAndTime = row.Field<DateTime>("dateAndTime"), Row = row })
.GroupBy(x => new { Date = x.DateAndTime.Date, Hour = x.DateAndTime.Date.Hour });
foreach (var x in hourGroups)
Console.WriteLine("Date: {0} Hour: {1}: Count: {2} All names: {3}",
x.Key.Date.ToShortDateString(),
x.Key.Hour,
x.Count(),
string.Join(",", x.Select(xx => xx.Row.Field<string>("name")))); // just bonus
Source: stackoverflow.com
Related Articles
- C# LINQ get records from datatable group by hour
- Use LINQ to group data from DataTable
- creating Linq to sqlite dbml from DbLinq source code
- How to select multiple columns from datatable in linq group by?
- linq Group By from DataTable
- LINQ get the data from the datatable group by hours
- Group by using Linq to get unique records from list
- Group By from DataTable using linq
- c# Linq or code to extract groups from a single list of source data
- Get records from datatable grouped by date and every two hour
- Fetch records with highest count from each group uniquely in Linq
- How to select only specific time records for each day from C# DataTable using linq
- Data paging in linq without removing records from the data source
- Filling a DataSet or a DataTable from a LINQ query result set
- Linq - Top value from each group
- Select distinct rows from datatable in Linq
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Datatable group by with linq in vb.net
- C# get minute number from date in linq group by
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- LINQ Select DataRow from DataTable
- Fastest way to fill DataTable from LINQ query using DataContext
- How to find duplicate record using Linq from DataTable
- c # using linq to group by multiple columns in a datatable
- select n records from nth record in linq
- How to pass LinQ Expressions from F# to C# code
- How to group by on multiple columns from datatable with linq?
- Using LINQ to delete an element from a ObservableCollection Source
- Joining two tables with LINQ while also returning null records from the second table
- Calculate duration between first login and last logout from a set of records in datatable
- How can I evaluate the result of a boolean expression in string format on runtime in C#?
- Ternary operator in LINQ query is not working as expected
- How to access child entity's property in a where clause of linq expression?
- LINQ Inner Join and Select Item Dropdown
- Join by LINQ to Entities (EF) same Table based on Time
- LINQ to SQL Transaction.Complete vs DataContext.SubmitChanges
- how to use LINQ TakeWhile and Join to a list
- Regex help: My regex pattern will match invalid Dictionary
- Linq to entities(latest entity framework) get many to many and one to many relation
- Querying a list in a list efficently
- DateDiff alternative in Linq
- in the statement that was created by LINQ , JOIN order has not been considered
- Full outer join of two string arrays
- LINQ query does not compile when using double, float, or decimal instead of int
- Can it be advantageous for a method to return IOrderedEnumerable<T> instead of IEnumerable<T>?
- Finding the different items in a list compared to its first item of the list
- LINQ Cannot assign void to Implicitly Typed variable
- Linq to CSV Not ouputting data
- Find first record based on start of string or until string is empty
- Problem with the communication of lambda expressions