C# Linq Datatable groupby datetime month - c#

I have a datatable that looks like this
The output I'm trying to achieve is described in the picture, where I want to group by the month of the datetime, and agent I suppose. This is what I have so far.
DataTable dtTemp = new DataTable();
dtTemp.Columns.Add("Agent", typeof(string));
dtTemp.Columns.Add("Amount", typeof(decimal));
dtTemp.Columns.Add("Date", typeof(DateTime));
dtTemp = dtTemp.AsEnumerable()
.GroupBy(r => r[0])
.Select(g =>
{
var row = dt.NewRow();
row[0] = g.Key;
row[1] = g.Sum(r => (decimal)r[1]);
return row;
})
.CopyToDataTable();
Any ideas how to achieve this?
Thanks in advance!

From analyzing your post, you need to group by data table rows by column Agent and only month and year from column Date.
You need to take one temporary data table dt that can hold data type for each column in your resultant data table result.
So CopyToDataTable() will copy all of your dtTemp group data to new result data table with temporary dt columns data type.
DataTable dt = new DataTable();
dt.Columns.Add("Agent", typeof(string));
dt.Columns.Add("Amount", typeof(decimal));
dt.Columns.Add("Date", typeof(string));
DataTable result = dtTemp.AsEnumerable()
.Select(x => new
{
Agent = x.Field<string>("Agent"),
Amount = x.Field<decimal>("Amount"),
Date = x.Field<DateTime>("Date").ToString("MM-yyyy")
})
.GroupBy(x => new { x.Agent, x.Date })
.Select(g =>
{
var r = dt.NewRow();
r["Agent"] = g.Key.Agent;
r["Amount"] = g.Sum(c => c.Amount);
r["Date"] = g.FirstOrDefault().Date;
return r;
})
.CopyToDataTable();
Output:

var temp = dtTemp.AsEnumerable().GroupBy(grp => new { grpmonth = Convert.ToDateTime(grp["Date"]).Month, grpyear = Convert.ToDateTime(grp["Date"]).Year, grpagent = grp["Agent"] })
.Select(val =>
{
var row = dtTemp.NewRow();
row["Agent"] = val.FirstOrDefault()["Agent"];
row["Amount"] = val.Sum(amt => Convert.ToDecimal(amt["Amount"]));
row["Date"] = val.FirstOrDefault()["Date"];
return row;
}
)
.CopyToDataTable();
For reference

So from your input sequence, you want all used Agents, with the total sum of the Amounts per month.
Let's assume your DataTable is a sequence of Rows, and that it easily can be converted to a sequence of Rows:
class RowData
{
public string Agent {get; set}
public DateTime Date {get; set;}
public int Amount {get; set;}
}
IEnumerable<RowData> tableData = ...
The solution if your problem is to make groups of RowData with equal value for Agent, and to group these groups again to make subgroups with equal value for year and month
var AgentsWithAmountsPerMonth = tableData
.GroupBy(row => row.Agent, // make groups of rows with same Agent
// ResultSelector: get the Agent (=key), with all rows that have this Agent
(agent, rowsWithThisAgent) => new
{
Agent = agent,
// to calculate the totals per year/month, extract the year / month / amount
TotalsPerMonth = rowsWithThisAgent.Select(row => new
{
Year = row.Date.Year,
Month = row.Date.Month,
Amount = row.Amount,
})
// and group by same Year / Month:
.GroupBy(row => new {row.Year, row.Month},
// ResultSelector
(yearMonth, rowsWithThisYearMonth) => new
{
Year = yearMonth.Year,
Month = yearMonth.Month,
Total = rowsWithThisYearMont.Select(row => row.Amount).Sum(),
// Or put the year and month in one field:
Month = new DateTime(yearMonth.Year, yearMonth.Month, 1),
},
});
});

Related

How can I display the count of Weekly BirthDate in c # chart?

var result = lst
.GroupBy(l => l.BirthDate)
.Select(cl => new
{
BirthDate = cl.First().BirthDate,
Count = cl.Count().ToString()
}).ToList();
DataTable dt = Utility.ConvertToDataTable(result);
chart2.DataSource = dt;
chart2.Name = "BirthDate";
chart2.Series["Series1"].XValueMember = "BirthDate";
chart2.Series["Series1"].YValueMembers = "Count";
this.chart2.Titles.Remove(this.chart1.Titles.FirstOrDefault());
this.chart2.Titles.Add("Weekly Enrollment Chart");
chart2.Series["Series1"].IsValueShownAsLabel = true;
This displays all the dates present in the datagridview.
I want to display the weekly Birthdate and count the number of births.
You can group by the week number in the year:
lst.GroupBy(b => {
var firstDayOfYear = new DateTime(b.BirthDate.Year, 1, 1);
// Calculate the days after which the first day
// of year appears so we can offset it when calculating week number
int daysAfterSunday = -1;
while(firstDayOfYear.DayOfWeek != DayOfWeek.Sunday) {
daysAfterSunday++;
firstDayOfYear = firstDayOfYear.AddDays(-1);
}
return (b.BirthDate.DayOfYear + daysAfterSunday)/7;
})
.Select(g => new { Week = g.Key, Count = g.Count(), BirthDates = g.Select(v => v.BirthDate) });
Then you can calculate the week start date from week number using the methods in this answer.

C# LINQ get records from datatable group by hour

I have a datatable whose columns are name, code, dateAndTime. Now I want to get the count of all the records for every hour in a Day using LINQ.
DateTime Column contains data as
2014-08-01 07:00:06.163
2014-08-01 07:00:11.873
2014-08-01 07:00:42.623
2014-08-01 07:00:48.363
2014-08-01 07:01:15.243
2014-08-01 07:01:16.507
Now I want to get the count of all the records from the start of the day to the end of the day hourly basis.
Example would be :
7 O Clock : 56 Records
8 O Clock : 90 Records
..
..
etc.
How it can be possible via LINQ ?
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()
});
I assume that the column really stores DateTimes, 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

using linq on datatable and putting result back into datatable with same format

What I m trying to do is relatively simple. I would like to use linq to compute some aggregated function on a group and then put the result back into a datatable of the same format. I did a lot of research and think I should use System.Data.DataSetExtensions and copy to datatable funtion. Here is my random datatable:
DataTable ADataTable = new DataTable("ADataTable");
// Fake table data
ADataTable.Columns.Add("PLANT", typeof(int));
ADataTable.Columns.Add("PDCATYPE_NAME", typeof(int));
ADataTable.Columns.Add("Month", typeof(int));
ADataTable.Columns.Add("Year", typeof(int));
ADataTable.Columns.Add("STATUS_NAME_REPORT", typeof(string));
ADataTable.Columns.Add("SAVINGS_PER_MONTH", typeof(double));
for (int i = 0; i < 15; i++)
{
for (int j = 1; j < 5; j++)
{
DataRow row = ADataTable.NewRow();
row["PLANT"] = j;
row["PDCATYPE_NAME"] = j;
row["Month"] = DateTime.Now.Month;
row["Year"] = DateTime.Now.Year;
row["STATUS_NAME_REPORT"] = "Report";
row["SAVINGS_PER_MONTH"] = j*i;
ADataTable.Rows.Add(row);
}
}
Now I will clone this format and do a simple sum on it via linq:
DataTable newtable = ADataTable.Clone();
// The actual query
IEnumerable<DataRow> query = (from rows in ADataTable.AsEnumerable()
group rows by new
{
PLANT = rows.Field<int>("PLANT"),
PDCATYPE_NAME = rows.Field<int>("PDCATYPE_NAME"),
Month = rows.Field<int>("Month"),
Year = rows.Field<int>("Year"),
STATUS_NAME_REPORT = rows.Field<string>("STATUS_NAME_REPORT")
} into g
select new
{
g.Key.PLANT,
g.Key.PDCATYPE_NAME,
g.Key.Month,
g.Key.Year,
g.Key.STATUS_NAME_REPORT,
sum = g.Sum(savings => savings.Field<double>("SAVINGS_PER_MONTH")),
});
newtable = query.CopyToDataTable<DataRow>();
The LINQ works fine but as soon as I put IEnumarable DataRow in front I get error that I cannot convert anonymys type to datarow. But if I put select new datarow I get an error that fields are unknown...
How do I proceed please?
You have multiple options, First is to use reflection to create a DataTable based on IEnumerable<T> and the other options is to populate your DataTable by enumerating your query like:
var query = ADataTable.AsEnumerable()
.GroupBy(row => new
{
PLANT = row.Field<int>("PLANT"),
PDCATYPE_NAME = row.Field<int>("PDCATYPE_NAME"),
Month = row.Field<int>("Month"),
Year = row.Field<int>("Year"),
STATUS_NAME_REPORT = row.Field<string>("STATUS_NAME_REPORT")
});
foreach (var g in query)
{
newtable.LoadDataRow(new object[]
{
g.Key.PLANT,
g.Key.PDCATYPE_NAME,
g.Key.Month,
g.Key.Year,
g.Key.STATUS_NAME_REPORT,
g.Sum(savings => savings.Field<double>("SAVINGS_PER_MONTH"))
}, LoadOption.OverwriteChanges);
}
The error in your code is because of selecting an anonymous type using select new and then trying to store it in IEnumerable<DataRow>. You can't specify DataRow in select as it is not accessible directly.
You may also see: How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow
This also works:
newtable2 = ADataTable.AsEnumerable().GroupBy(a => new
{
PLANT = a.Field<int>("PLANT"),
PDCATYPE_NAME = a.Field<int>("PDCATYPE_NAME"),
Month = a.Field<int>("Month"),
Year = a.Field<int>("Year"),
STATUS_NAME_REPORT = a.Field<string>("STATUS_NAME_REPORT")
}).Select(g =>
{
var row = newtable2.NewRow();
row.ItemArray = new object[]
{
g.Key.PLANT,
g.Key.PDCATYPE_NAME,
g.Key.Month,
g.Key.Year,
g.Key.STATUS_NAME_REPORT,
g.Sum(r => r.Field<double>("SAVINGS_PER_MONTH"))
};
return row;
}).CopyToDataTable();
using System.Data.DataSetExtensions (Which requires a reference)

Remove rows with same column value from DataTable and add corresponding values

I have a DataTable with multiple columns. If the value of certain column repeats, I need to remove that row and add the quantities against it. For example, following datatable
ITEM QTY
------------
1 20
2 10
2 10
3 20
would become:
ITEM QTY
-----------
1 20
2 20
3 20
This is what I did
var table = dt.AsEnumerable()
.GroupBy(row => row.Field("ITEM"))
.Select(group => group.First())
.CopyToDataTable();
It removes the extra row but doesn't add up the quantities. So please help me in this regard.
You can use Sum. You just have to find the duplicate-rows first:
var dupGroups = dt.AsEnumerable()
.GroupBy(row => row.Field<int>("ITEM"))
.Where(g => g.Count() > 1);
Now you can use them to get the sum and to remove the redundant rows from the table.
foreach (var group in dupGroups)
{
DataRow first = group.First();
int sum = group.Sum(r => r.Field<int>("QTY"));
first.SetField("QTY", sum);
foreach (DataRow row in group.Skip(1))
dt.Rows.Remove(row);
}
Or in one query which creates a new DataTable.
DataTable newTable = dt.AsEnumerable()
.GroupBy(row => row.Field<int>("ITEM"))
.Select(g =>
{
DataRow first = g.First();
if (g.Count() > 1)
{
int sum = g.Sum(r => r.Field<int>("QTY"));
first.SetField("QTY", sum);
}
return first;
})
.CopyToDataTable();
However, even the second approach modifies the original table which might be undesired since you use CopyToDatatable to create a new DataTable. You need to clone the original table(DataTable newTable = dt.Clone();) to get an empty table with the same schema. Then use NewRow + ItemArray.Clone() or table.ImportRow to create a real clone without modifying the original data.
See: C# simple way to copy or clone a DataRow?
Edit: Here is an example how you can create a clone without touching the original table:
DataTable newTable = dt.Clone();
var itemGroups = dt.AsEnumerable()
.GroupBy(row => row.Field<int>("ITEM"));
foreach (var group in itemGroups)
{
DataRow first = group.First();
if (group.Count() == 1)
newTable.ImportRow(first);
else
{
DataRow clone = newTable.Rows.Add((object[])first.ItemArray.Clone());
int qtySum = group.Sum(r => r.Field<int>("QTY"));
clone.SetField("QTY", qtySum);
}
}
var table = dt.AsEnumerable()
.GroupBy(row => row.Field<int>("ITEM"))
.Select(group => {
var row = group.First();
row['QTY'] = group.Sum(x => x.Field<int>('QTY'));
return row;
}).CopyToDataTable();
This won't change your original DataTable:
var table = dt.Copy().AsEnumerable()
.GroupBy(row=>row["ITEM"])
.Select(g=> {
DataRow dr = g.First();
dr.SetField("QTY", g.Sum(x=>x.Field<int>("QTY")));
return dr;
})
.CopyToDataTable();

how to find the max in a datacolumn of datetime?

I have a DataColumn of DateTime, I would like to know how I can have only the sooner date (min) and the later date (max).
Thanks
object maxDate = dataTable.Compute("MAX(TheDateColumnName)", null);
object minDate = dataTable.Compute("MIN(TheDateColumnName)", null);
This would give what you are looking for:
// Initial Code for Testing
DataTable dt = new DataTable();
dt.Columns.Add("Dates", typeof(DateTime));
dt.Rows.Add(new object[] { DateTime.Now });
dt.Rows.Add(new object[] { DateTime.Now.AddDays(1) });
dt.Rows.Add(new object[] { DateTime.Now.AddDays(2) });
This is the code you would use:
// Actual Code
DataColumn col = dt.Columns[0]; // Call this the one you have
DataTable tbl = col.Table;
var first = tbl.AsEnumerable()
.Select(cols => cols.Field<DateTime>(col.ColumnName))
.OrderBy(p => p.Ticks)
.FirstOrDefault();
var last = tbl.AsEnumerable()
.Select(cols => cols.Field<DateTime>(col.ColumnName))
.OrderByDescending(p => p.Ticks)
.FirstOrDefault();
To add to the answer from kyle, isn't it easier to just do:
for greatest date:
var last = tbl.AsEnumerable()
.Max(r => r.Field<DateTime>(col.ColumnName));
and for earliestdate:
var first = tbl.AsEnumerable()
.Min(r => r.Field<DateTime>(col.ColumnName));
Just retreive a List of DateTime from your DataColumn,
Foreach row in your DataColumn add the current element to your List of DateTime.
List<DateTime> and use Sort method then get the first and the last values.
Depending of your framework version, for 2.0 use above, for >=3.5
you can use Max and Min
or
With linq .OrderByDesc(p => p.X).FirstOrDefault(); on your DateTime List
DataTable dt = new DataTable("MyDataTable");
DataColumn dc = new DataColumn("DateColumn");
dc.DataType = typeof(DateTime);
dt.Columns.Add(dc);
for (int i = 0; i <= 5; i++)
{
DataRow newRow = dt.NewRow();
newRow[0] = DateTime.Now.AddDays(i);
dt.Rows.Add(newRow);
}
DateTime maxDate =
Convert.ToDateTime(
((from DataRow dr in dt.Rows
orderby Convert.ToDateTime(dr["DateColumn"]) descending
select dr).FirstOrDefault()["DateColumn"]
)
);
DateTime minDate =
Convert.ToDateTime(
((from DataRow dr in dt.Rows
orderby Convert.ToDateTime(dr["DateColumn"]) ascending
select dr).FirstOrDefault()["DateColumn"]
)
);
Simpliest for me:
make a dataset with a Table "Tablename" and a column "itemDate" ,set Datatyp System.DateTime
you can read all elements in a list of datetime and search for minDate
Dim List_Date As New List(Of DateTime)
Dim minDate As DateTime
For Each elem As DataRow In DataSet1.Tables("Tablename").Rows
List_Date.Add(elem.Item("itemDate"))
Next
minDate = List_Date.Min

Categories