I have a datatable which comprises values from 1st January to march, something like this: - DataTable datatable = new DataTable("Employee");
-
datatable.Columns.Add("Date", typeof(string));
-
datatable.Columns.Add("Employee", typeof(string));
-
datatable.Columns.Add("Job1", typeof(double));
-
datatable.Columns.Add("Job2", typeof(double));
-
-
datatable.Rows.Add(new Object[] { "1/4/2013", "A", 1.3, 2 });
-
datatable.Rows.Add(new Object[] { "1/4/2013", "B", 2.5, 6 });
-
datatable.Rows.Add(new Object[] { "1/6/2013", "C", 3.7, 2.4 });
-
datatable.Rows.Add(new Object[] { "1/7/2013", "D", 11, 0.0 });
-
datatable.Rows.Add(new Object[] { "1/7/2013", "F", 334, 0 });
-
datatable.Rows.Add(new Object[] { "1/8/2013", "A", 1.87, 1 });
-
datatable.Rows.Add(new Object[] { "1/8/2013", "B", 6.85, 2 });
-
datatable.Rows.Add(new Object[] { "1/9/2013", "C", 58, 226 });
-
datatable.Rows.Add(new Object[] { "1/16/2013", "A", 9.43, 1.45 });
-
datatable.Rows.Add(new Object[] { "1/16/2013", "B", 5.27, 0.6 });
-
datatable.Rows.Add(new Object[] { "1/22/2013", "C", 45.4, 5 });
-
datatable.Rows.Add(new Object[] { "1/23/2013", "A", 44, 4.78 });
-
datatable.Rows.Add(new Object[] { "1/29/2013", "B", 45, 40 });
-
datatable.Rows.Add(new Object[] { "2/2/2013", "C", 45, 54.12 });
-
datatable.Rows.Add(new Object[] { "2/2/2013", "D", 7, 4.4587 });
-
datatable.Rows.Add(new Object[] { "2/3/2013", "F", "265", 11.486 });
-
datatable.Rows.Add(new Object[] { "3/3/2013", "A", "25", 28.124 });
The result should look like this for Job1:
Employee 1/7-1/13 1/14-1/20 1/21-1/27 1/28-2/3 and so on...
A sum of values for this 7 days
B
C
D
I want to add all the values of Job1 for each employee in a time range which is one week starting from monday to sunday.
Please suggest how can I group the Job1 values week wise and store them in another datatable.
4 5805
Hey Radha Shyam, try this code (I changed datatype of 'Date' field to 'DateTime' from 'string' and Table name to 'EmployeeTable' from 'Employee' because already you've a field called as 'Employee' to save employee names in your code) - using System;
-
using System.Collections.Generic;
-
using System.Data;
-
using System.Linq;
-
using System.Windows.Forms;
-
-
namespace WindowsFormsApplication1
-
{
-
public partial class Form1 : Form
-
{
-
DataTable datatable;
-
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
-
private void Form1_Load(object sender, EventArgs e)
-
{
-
DateTime StartDate = new DateTime(2012, 12, 31); // 12/31/2012
-
DateTime EndDate = new DateTime(2013, 3, 3); // 03/03/2013
-
-
DateTime StartWeekDate = StartDate;
-
DateTime EndWeekDate = StartWeekDate.AddDays(6);
-
-
datatable = new DataTable("EmployeeTable");
-
datatable.Columns.Add("Date", typeof(DateTime));
-
datatable.Columns.Add("Employee", typeof(string));
-
datatable.Columns.Add("Job1", typeof(double));
-
datatable.Columns.Add("Job2", typeof(double));
-
-
datatable.Rows.Add(new Object[] { "1/4/2013", "A", 1.3, 2 });
-
datatable.Rows.Add(new Object[] { "1/4/2013", "B", 2.5, 6 });
-
datatable.Rows.Add(new Object[] { "1/6/2013", "C", 3.7, 2.4 });
-
datatable.Rows.Add(new Object[] { "1/7/2013", "D", 11, 0.0 });
-
datatable.Rows.Add(new Object[] { "1/7/2013", "F", 334, 0 });
-
datatable.Rows.Add(new Object[] { "1/8/2013", "A", 1.87, 1 });
-
datatable.Rows.Add(new Object[] { "1/8/2013", "B", 6.85, 2 });
-
datatable.Rows.Add(new Object[] { "1/9/2013", "C", 58, 226 });
-
datatable.Rows.Add(new Object[] { "1/16/2013", "A", 9.43, 1.45 });
-
datatable.Rows.Add(new Object[] { "1/16/2013", "B", 5.27, 0.6 });
-
datatable.Rows.Add(new Object[] { "1/22/2013", "C", 45.4, 5 });
-
datatable.Rows.Add(new Object[] { "1/23/2013", "A", 44, 4.78 });
-
datatable.Rows.Add(new Object[] { "1/29/2013", "B", 45, 40 });
-
datatable.Rows.Add(new Object[] { "2/2/2013", "C", 45, 54.12 });
-
datatable.Rows.Add(new Object[] { "2/2/2013", "D", 7, 4.4587 });
-
datatable.Rows.Add(new Object[] { "2/3/2013", "F", 265, 11.486 });
-
datatable.Rows.Add(new Object[] { "3/3/2013", "A", 25, 28.124 });
-
-
for (DateTime currentDate = StartWeekDate; currentDate <= EndDate; )
-
{
-
calculate(currentDate, EndWeekDate, "A");
-
calculate(currentDate, EndWeekDate, "B");
-
calculate(currentDate, EndWeekDate, "C");
-
calculate(currentDate, EndWeekDate, "D");
-
calculate(currentDate, EndWeekDate, "F");
-
-
currentDate = currentDate.AddDays(7);
-
EndWeekDate = currentDate.AddDays(6);
-
}
-
}
-
-
private void calculate(DateTime StartWeekDate, DateTime EndWeekDate, string name)
-
{
-
List<TempClass> list = new List<TempClass>();
-
-
Console.WriteLine(name + " " + StartWeekDate.ToShortDateString() + " " + EndWeekDate.ToShortDateString() + " ");
-
-
Console.WriteLine(datatable.AsEnumerable().Select(a => new TempClass
-
{
-
Date = a.Field<DateTime>("Date"),
-
Employee = a.Field<string>("Employee"),
-
Job1 = a.Field<double>("Job1"),
-
Job2 = a.Field<double>("Job2")
-
}).Where(b => b.Date >= StartWeekDate && b.Date <= EndWeekDate).Where(c => c.Employee == name).Sum(c => c.Job1).ToString());
-
-
Console.WriteLine(datatable.AsEnumerable().Select(a => new TempClass
-
{
-
Date = a.Field<DateTime>("Date"),
-
Employee = a.Field<string>("Employee"),
-
Job1 = a.Field<double>("Job1"),
-
Job2 = a.Field<double>("Job2")
-
}).Where(b => b.Date >= StartWeekDate && b.Date <= EndWeekDate).Where(c => c.Employee == name).Sum(c => c.Job2).ToString());
-
}
-
}
-
-
public class TempClass
-
{
-
public DateTime Date
-
{
-
get;
-
set;
-
}
-
-
public string Employee
-
{
-
get;
-
set;
-
}
-
public double Job1
-
{
-
get;
-
set;
-
}
-
public double Job2
-
{
-
get;
-
set;
-
}
-
}
-
}
Thank you for your reply but this does not solve my problem. I want to have columns where week starts from 1/7 - 1/13 and in that column the values for Job1 is aggregated for this particular week. I want this all stored in a datatable as i have to do further calculation with these values so no writing on consoles. Anyhow, appreciate your help. Thanks!
Hey Radha Shyam, i thought you may write that simple part of the code by yourself. Check the following code, i modified it for you. TextBox 'textbox' is for your reference, if you want you can remove it. - using System;
-
using System.Collections.Generic;
-
using System.Data;
-
using System.Drawing;
-
using System.Linq;
-
using System.Windows.Forms;
-
-
namespace WindowsFormsApplication1
-
{
-
public partial class Form1 : Form
-
{
-
DataTable datatable;
-
TextBox textbox;
-
DataTable GroupByWeek;
-
List<TempClass> list;
-
double j1, j2;
-
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
-
private void Form1_Load(object sender, EventArgs e)
-
{
-
DateTime StartDate = new DateTime(2012, 12, 31);
-
DateTime EndDate = new DateTime(2013, 3, 3);
-
-
DateTime StartWeekDate = StartDate;
-
DateTime EndWeekDate = StartWeekDate.AddDays(6);
-
-
datatable = new DataTable("EmployeeTable");
-
datatable.Columns.Add("Date", typeof(DateTime));
-
datatable.Columns.Add("Employee", typeof(string));
-
datatable.Columns.Add("Job1", typeof(double));
-
datatable.Columns.Add("Job2", typeof(double));
-
-
datatable.Rows.Add(new Object[] { "1/4/2013", "A", 1.3, 2 });
-
datatable.Rows.Add(new Object[] { "1/4/2013", "B", 2.5, 6 });
-
datatable.Rows.Add(new Object[] { "1/6/2013", "C", 3.7, 2.4 });
-
datatable.Rows.Add(new Object[] { "1/7/2013", "D", 11, 0.0 });
-
datatable.Rows.Add(new Object[] { "1/7/2013", "F", 334, 0 });
-
datatable.Rows.Add(new Object[] { "1/8/2013", "A", 1.87, 1 });
-
datatable.Rows.Add(new Object[] { "1/8/2013", "B", 6.85, 2 });
-
datatable.Rows.Add(new Object[] { "1/9/2013", "C", 58, 226 });
-
datatable.Rows.Add(new Object[] { "1/16/2013", "A", 9.43, 1.45 });
-
datatable.Rows.Add(new Object[] { "1/16/2013", "B", 5.27, 0.6 });
-
datatable.Rows.Add(new Object[] { "1/22/2013", "C", 45.4, 5 });
-
datatable.Rows.Add(new Object[] { "1/23/2013", "A", 44, 4.78 });
-
datatable.Rows.Add(new Object[] { "1/29/2013", "B", 45, 40 });
-
datatable.Rows.Add(new Object[] { "2/2/2013", "C", 45, 54.12 });
-
datatable.Rows.Add(new Object[] { "2/2/2013", "D", 7, 4.4587 });
-
datatable.Rows.Add(new Object[] { "2/3/2013", "F", 265, 11.486 });
-
datatable.Rows.Add(new Object[] { "3/3/2013", "A", 25, 28.124 });
-
-
textbox = new TextBox();
-
textbox.Width = 250;
-
textbox.Height = 200;
-
textbox.Multiline = true;
-
textbox.ScrollBars = ScrollBars.Vertical;
-
textbox.Location = new Point(12, 12);
-
this.Controls.Add(textbox);
-
-
textbox.Text = String.Empty;
-
-
GroupByWeek = new DataTable("GroupByWeek");
-
GroupByWeek.Columns.Add("Employee", typeof(string));
-
GroupByWeek.Columns.Add("From", typeof(DateTime));
-
GroupByWeek.Columns.Add("To", typeof(DateTime));
-
GroupByWeek.Columns.Add("Job1", typeof(double));
-
GroupByWeek.Columns.Add("Job2", typeof(double));
-
-
for (DateTime currentDate = StartWeekDate; currentDate <= EndDate; )
-
{
-
calculate(currentDate, EndWeekDate, "A");
-
calculate(currentDate, EndWeekDate, "B");
-
calculate(currentDate, EndWeekDate, "C");
-
calculate(currentDate, EndWeekDate, "D");
-
calculate(currentDate, EndWeekDate, "F");
-
-
textbox.Text += Environment.NewLine;
-
-
currentDate = currentDate.AddDays(7);
-
EndWeekDate = currentDate.AddDays(6);
-
}
-
}
-
-
void calculate(DateTime StartWeekDate, DateTime EndWeekDate, string name)
-
{
-
list = new List<TempClass>();
-
-
j1 = datatable.AsEnumerable().Select(a => new TempClass
-
{
-
Date = a.Field<DateTime>("Date"),
-
Employee = a.Field<string>("Employee"),
-
Job1 = a.Field<double>("Job1"),
-
Job2 = a.Field<double>("Job2")
-
}).Where(b => b.Date >= StartWeekDate && b.Date <= EndWeekDate).Where(c => c.Employee == name).Sum(c => c.Job1);
-
-
j2 = datatable.AsEnumerable().Select(a => new TempClass
-
{
-
Date = a.Field<DateTime>("Date"),
-
Employee = a.Field<string>("Employee"),
-
Job1 = a.Field<double>("Job1"),
-
Job2 = a.Field<double>("Job2")
-
}).Where(b => b.Date >= StartWeekDate && b.Date <= EndWeekDate).Where(c => c.Employee == name).Sum(c => c.Job2);
-
-
GroupByWeek.Rows.Add(new Object[] { name, StartWeekDate.ToShortDateString(), EndWeekDate.ToShortDateString(), j1, j2 });
-
textbox.Text += name + " " + StartWeekDate.ToShortDateString() + " " + EndWeekDate.ToShortDateString() + " " + j1 + " " + j2 + Environment.NewLine;
-
}
-
}
-
-
public class TempClass
-
{
-
public DateTime Date
-
{
-
get;
-
set;
-
}
-
-
public string Employee
-
{
-
get;
-
set;
-
}
-
public double Job1
-
{
-
get;
-
set;
-
}
-
public double Job2
-
{
-
get;
-
set;
-
}
-
}
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Job Lot |
last post by:
Is it possible to use Aggregate functions with GROUP BY Clauses on DataTable. I have a DataTable with following values:
Date Amount Int Balance
1/1/2004 5000.00 50.00 5050.00...
|
by: =?Utf-8?B?dHZpbg==?= |
last post by:
Hi everybody
i have a datatable, i want to select distinct value from datatable and not
database.
so is there any method that can provide this query:
Select DISTINCT from DataTable
please help...
|
by: JoaquimC |
last post by:
Hello,
I need to delete a group of rows from a DataTable based on a condition. Something like:
string CRefeicao = "01", CFamiliaPrato = "02";
foreach (DataRow LinhaDetalhe in Detalhe.Rows)...
|
by: ranganadh |
last post by:
Dear Group members,
I am new to LINQ, pls help on the deeling with huge amount of data
with the C# stand Alone application.
I have two file, which contains more then 2 lacs lines in every...
|
by: Neil |
last post by:
Hi group,
is there a special C# LINQ group available?
Regards
| |
by: Marc Gravell |
last post by:
I hadn't yet needed to do this, but I'm kinda glad it works! Shame about
all those DATEADDs though... I've just run a test, and a
CAST(FLOOR(CAST(x as float)) as datetime)
is twice as quick......
|
by: Andy B |
last post by:
Hi...
I have this DataTable: StockContract.Dictionary. I need a linq to DataTable
query that will select all rows where the "Word" column values match the
WordTextBox.Text value. I am running...
|
by: shapper |
last post by:
Hello,
I am used to SQL but I am starting to use LINQ.
How can I create Left, Right and Inner joins in LINQ? How to
distinguish the different joins?
Here is a great SQL example:...
|
by: Seb |
last post by:
I want to count activity in a given month. I'm trying to do so with
the linq code below however it reports:
Error 1 'a' is inaccessible due to its protection level
var ActivityByMonths = from a...
|
by: Peter Morris |
last post by:
responseData.Signals is a List<SignalInfo>
var signalGroups =
from signal in responseData.Signals
where signal.DisplayPath.StartsWith("AppMenu")
orderby signal.DisplayPath
group signal by...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |