combining data from two tables in a database to get total sum (C#)
I'm new to databases, and I need help. I have a database with two tables ([tb_TotalH], [tb_PricePerH]). The first table [tb_TotalH] shows the total number of hours per day, and in the second table [tb_PricePerH] are the prices of the hour with the date they were created.
I need to calculate the total amount (hours per day multiply price per hour).
But the trick is that the price of the hour is not fixed, it changes and I need a price in the period when working hours were made. And I also need to get the price when I make query for filter of dates to get the price in label.
I have no idea how to start this!
This is some of my code that I've done so far:
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=|DataDirectory|Database1.mdf;Integrated Security=True");
void DisplayData()
{
con.Open();
DataTable dt = new DataTable();
sda = new SqlDataAdapter("select * from tb_TotalH", con);
sda.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AutoResizeColumns();
}
void BetweenDates()
{
DateTime dtFrom = Convert.ToDateTime(dtp1.Value);
DateTime dtTo = Convert.ToDateTime(dtp2.Value);
SqlDataAdapter mda = new SqlDataAdapter("select * from DailyDataEntry where Date_H between '" + dtFrom.ToShortDateString() + "' and '" + dtTo.ToShortDateString() + "' ", con);
System.Data.DataSet ds = new System.Data.DataSet();
con.Open();
mda.Fill(ds, "root");
dataGridView1.DataSource = ds.Tables["root"];
dataGridView1.Refresh();
con.Close();
}
void getPrice()
{
SqlDataReader rdr = null;
SqlCommand cmd = null;
con.Open();
string CommandText = "SELECT Price, Tax FROM tb_PricePerH WHERE Date_M = @date";
cmd = new SqlCommand(CommandText);
cmd.Connection = con;
cmd.Parameters.AddWithValue("@date", "4/22/2018");
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
lbl1.Text = Convert.ToDouble(rdr["PricePerHour"].ToString());
lbl2.Text = Convert.ToDouble(rdr["Tax"].ToString());
}
con.Close();
}
c# sql .net database
add a comment |
I'm new to databases, and I need help. I have a database with two tables ([tb_TotalH], [tb_PricePerH]). The first table [tb_TotalH] shows the total number of hours per day, and in the second table [tb_PricePerH] are the prices of the hour with the date they were created.
I need to calculate the total amount (hours per day multiply price per hour).
But the trick is that the price of the hour is not fixed, it changes and I need a price in the period when working hours were made. And I also need to get the price when I make query for filter of dates to get the price in label.
I have no idea how to start this!
This is some of my code that I've done so far:
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=|DataDirectory|Database1.mdf;Integrated Security=True");
void DisplayData()
{
con.Open();
DataTable dt = new DataTable();
sda = new SqlDataAdapter("select * from tb_TotalH", con);
sda.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AutoResizeColumns();
}
void BetweenDates()
{
DateTime dtFrom = Convert.ToDateTime(dtp1.Value);
DateTime dtTo = Convert.ToDateTime(dtp2.Value);
SqlDataAdapter mda = new SqlDataAdapter("select * from DailyDataEntry where Date_H between '" + dtFrom.ToShortDateString() + "' and '" + dtTo.ToShortDateString() + "' ", con);
System.Data.DataSet ds = new System.Data.DataSet();
con.Open();
mda.Fill(ds, "root");
dataGridView1.DataSource = ds.Tables["root"];
dataGridView1.Refresh();
con.Close();
}
void getPrice()
{
SqlDataReader rdr = null;
SqlCommand cmd = null;
con.Open();
string CommandText = "SELECT Price, Tax FROM tb_PricePerH WHERE Date_M = @date";
cmd = new SqlCommand(CommandText);
cmd.Connection = con;
cmd.Parameters.AddWithValue("@date", "4/22/2018");
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
lbl1.Text = Convert.ToDouble(rdr["PricePerHour"].ToString());
lbl2.Text = Convert.ToDouble(rdr["Tax"].ToString());
}
con.Close();
}
c# sql .net database
could you show sample output ?
– Anu Viswan
Jan 1 at 2:20
It appears that no one can work over midnight, so the problem becomes one of finding the applicable hourly rate for a given date. Is the intent that the hourly rate is the one for: (1) the date in question, if it exists, or (B) the latest priced date prior to the date in question? Since you haven't explained how to calculate the total sum it's a bit of a challenge for us to help you. Aside: You convert numeric values to strings to doubles to strings for display in labels? Tip: Reasons not to use images are here.
– HABO
Jan 1 at 2:22
@HABO If you do not want to help you do not have to, and the question was not for that strings,doubles,strings, code is in the test phase. And any help even if is not in code is welcome, and if I had experience, I would not ask the question here.
– s.s.
Jan 1 at 2:45
@Anu Viswan For example, on 07.05.2018 the total hours for this day was 9h, in that period (from tb_PricePerH) the price of the hour was 11, because it started 06.19.2018. So on this day the profit was 99.
– s.s.
Jan 1 at 2:46
You may find this answer to be helpful.
– HABO
Jan 1 at 4:42
add a comment |
I'm new to databases, and I need help. I have a database with two tables ([tb_TotalH], [tb_PricePerH]). The first table [tb_TotalH] shows the total number of hours per day, and in the second table [tb_PricePerH] are the prices of the hour with the date they were created.
I need to calculate the total amount (hours per day multiply price per hour).
But the trick is that the price of the hour is not fixed, it changes and I need a price in the period when working hours were made. And I also need to get the price when I make query for filter of dates to get the price in label.
I have no idea how to start this!
This is some of my code that I've done so far:
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=|DataDirectory|Database1.mdf;Integrated Security=True");
void DisplayData()
{
con.Open();
DataTable dt = new DataTable();
sda = new SqlDataAdapter("select * from tb_TotalH", con);
sda.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AutoResizeColumns();
}
void BetweenDates()
{
DateTime dtFrom = Convert.ToDateTime(dtp1.Value);
DateTime dtTo = Convert.ToDateTime(dtp2.Value);
SqlDataAdapter mda = new SqlDataAdapter("select * from DailyDataEntry where Date_H between '" + dtFrom.ToShortDateString() + "' and '" + dtTo.ToShortDateString() + "' ", con);
System.Data.DataSet ds = new System.Data.DataSet();
con.Open();
mda.Fill(ds, "root");
dataGridView1.DataSource = ds.Tables["root"];
dataGridView1.Refresh();
con.Close();
}
void getPrice()
{
SqlDataReader rdr = null;
SqlCommand cmd = null;
con.Open();
string CommandText = "SELECT Price, Tax FROM tb_PricePerH WHERE Date_M = @date";
cmd = new SqlCommand(CommandText);
cmd.Connection = con;
cmd.Parameters.AddWithValue("@date", "4/22/2018");
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
lbl1.Text = Convert.ToDouble(rdr["PricePerHour"].ToString());
lbl2.Text = Convert.ToDouble(rdr["Tax"].ToString());
}
con.Close();
}
c# sql .net database
I'm new to databases, and I need help. I have a database with two tables ([tb_TotalH], [tb_PricePerH]). The first table [tb_TotalH] shows the total number of hours per day, and in the second table [tb_PricePerH] are the prices of the hour with the date they were created.
I need to calculate the total amount (hours per day multiply price per hour).
But the trick is that the price of the hour is not fixed, it changes and I need a price in the period when working hours were made. And I also need to get the price when I make query for filter of dates to get the price in label.
I have no idea how to start this!
This is some of my code that I've done so far:
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=|DataDirectory|Database1.mdf;Integrated Security=True");
void DisplayData()
{
con.Open();
DataTable dt = new DataTable();
sda = new SqlDataAdapter("select * from tb_TotalH", con);
sda.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AutoResizeColumns();
}
void BetweenDates()
{
DateTime dtFrom = Convert.ToDateTime(dtp1.Value);
DateTime dtTo = Convert.ToDateTime(dtp2.Value);
SqlDataAdapter mda = new SqlDataAdapter("select * from DailyDataEntry where Date_H between '" + dtFrom.ToShortDateString() + "' and '" + dtTo.ToShortDateString() + "' ", con);
System.Data.DataSet ds = new System.Data.DataSet();
con.Open();
mda.Fill(ds, "root");
dataGridView1.DataSource = ds.Tables["root"];
dataGridView1.Refresh();
con.Close();
}
void getPrice()
{
SqlDataReader rdr = null;
SqlCommand cmd = null;
con.Open();
string CommandText = "SELECT Price, Tax FROM tb_PricePerH WHERE Date_M = @date";
cmd = new SqlCommand(CommandText);
cmd.Connection = con;
cmd.Parameters.AddWithValue("@date", "4/22/2018");
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
lbl1.Text = Convert.ToDouble(rdr["PricePerHour"].ToString());
lbl2.Text = Convert.ToDouble(rdr["Tax"].ToString());
}
con.Close();
}
c# sql .net database
c# sql .net database
asked Jan 1 at 1:51
s.s.s.s.
3117
3117
could you show sample output ?
– Anu Viswan
Jan 1 at 2:20
It appears that no one can work over midnight, so the problem becomes one of finding the applicable hourly rate for a given date. Is the intent that the hourly rate is the one for: (1) the date in question, if it exists, or (B) the latest priced date prior to the date in question? Since you haven't explained how to calculate the total sum it's a bit of a challenge for us to help you. Aside: You convert numeric values to strings to doubles to strings for display in labels? Tip: Reasons not to use images are here.
– HABO
Jan 1 at 2:22
@HABO If you do not want to help you do not have to, and the question was not for that strings,doubles,strings, code is in the test phase. And any help even if is not in code is welcome, and if I had experience, I would not ask the question here.
– s.s.
Jan 1 at 2:45
@Anu Viswan For example, on 07.05.2018 the total hours for this day was 9h, in that period (from tb_PricePerH) the price of the hour was 11, because it started 06.19.2018. So on this day the profit was 99.
– s.s.
Jan 1 at 2:46
You may find this answer to be helpful.
– HABO
Jan 1 at 4:42
add a comment |
could you show sample output ?
– Anu Viswan
Jan 1 at 2:20
It appears that no one can work over midnight, so the problem becomes one of finding the applicable hourly rate for a given date. Is the intent that the hourly rate is the one for: (1) the date in question, if it exists, or (B) the latest priced date prior to the date in question? Since you haven't explained how to calculate the total sum it's a bit of a challenge for us to help you. Aside: You convert numeric values to strings to doubles to strings for display in labels? Tip: Reasons not to use images are here.
– HABO
Jan 1 at 2:22
@HABO If you do not want to help you do not have to, and the question was not for that strings,doubles,strings, code is in the test phase. And any help even if is not in code is welcome, and if I had experience, I would not ask the question here.
– s.s.
Jan 1 at 2:45
@Anu Viswan For example, on 07.05.2018 the total hours for this day was 9h, in that period (from tb_PricePerH) the price of the hour was 11, because it started 06.19.2018. So on this day the profit was 99.
– s.s.
Jan 1 at 2:46
You may find this answer to be helpful.
– HABO
Jan 1 at 4:42
could you show sample output ?
– Anu Viswan
Jan 1 at 2:20
could you show sample output ?
– Anu Viswan
Jan 1 at 2:20
It appears that no one can work over midnight, so the problem becomes one of finding the applicable hourly rate for a given date. Is the intent that the hourly rate is the one for: (1) the date in question, if it exists, or (B) the latest priced date prior to the date in question? Since you haven't explained how to calculate the total sum it's a bit of a challenge for us to help you. Aside: You convert numeric values to strings to doubles to strings for display in labels? Tip: Reasons not to use images are here.
– HABO
Jan 1 at 2:22
It appears that no one can work over midnight, so the problem becomes one of finding the applicable hourly rate for a given date. Is the intent that the hourly rate is the one for: (1) the date in question, if it exists, or (B) the latest priced date prior to the date in question? Since you haven't explained how to calculate the total sum it's a bit of a challenge for us to help you. Aside: You convert numeric values to strings to doubles to strings for display in labels? Tip: Reasons not to use images are here.
– HABO
Jan 1 at 2:22
@HABO If you do not want to help you do not have to, and the question was not for that strings,doubles,strings, code is in the test phase. And any help even if is not in code is welcome, and if I had experience, I would not ask the question here.
– s.s.
Jan 1 at 2:45
@HABO If you do not want to help you do not have to, and the question was not for that strings,doubles,strings, code is in the test phase. And any help even if is not in code is welcome, and if I had experience, I would not ask the question here.
– s.s.
Jan 1 at 2:45
@Anu Viswan For example, on 07.05.2018 the total hours for this day was 9h, in that period (from tb_PricePerH) the price of the hour was 11, because it started 06.19.2018. So on this day the profit was 99.
– s.s.
Jan 1 at 2:46
@Anu Viswan For example, on 07.05.2018 the total hours for this day was 9h, in that period (from tb_PricePerH) the price of the hour was 11, because it started 06.19.2018. So on this day the profit was 99.
– s.s.
Jan 1 at 2:46
You may find this answer to be helpful.
– HABO
Jan 1 at 4:42
You may find this answer to be helpful.
– HABO
Jan 1 at 4:42
add a comment |
1 Answer
1
active
oldest
votes
I have tried to mock your scenario with List.
var tablePricePerHours = new List<TablePricePerHours>
{
new TablePricePerHours{ Price = 10, Tax = 19, Date = new DateTime(2018,1,5)},
new TablePricePerHours{ Price = 10.5, Tax = 21.5, Date = new DateTime(2018,4,22)},
new TablePricePerHours{ Price = 11, Tax = 22.5, Date = new DateTime(2018,6,19)},
new TablePricePerHours{ Price = 11.5, Tax = 23, Date = new DateTime(2018,10,30)},
new TablePricePerHours{ Price = 12, Tax = 23.2, Date = new DateTime(2018,11,1)}
};
var tableTotalHours = new List<TableTotalHours>
{
new TableTotalHours{ Date = new DateTime(2018,02,15), TotalHours = 5},
new TableTotalHours{ Date = new DateTime(2018,02,19), TotalHours =10},
new TableTotalHours{ Date = new DateTime(2018,02,25), TotalHours = 8},
new TableTotalHours{ Date = new DateTime(2018,03,29), TotalHours = 7.5},
new TableTotalHours{ Date = new DateTime(2018,07,05), TotalHours = 9},
new TableTotalHours{ Date = new DateTime(2018,07,06), TotalHours = 1.5},
new TableTotalHours{ Date = new DateTime(2018,07,07), TotalHours = 12},
new TableTotalHours{ Date = new DateTime(2018,10,22), TotalHours = 10},
new TableTotalHours{ Date = new DateTime(2018,10,22), TotalHours = 10},
new TableTotalHours{ Date = new DateTime(2018,11,1), TotalHours = 8},
new TableTotalHours{ Date = new DateTime(2018,12,21), TotalHours = 8.5},
new TableTotalHours{ Date = new DateTime(2018,12,22), TotalHours = 9},
};
Where TablePricePerHours and TableTotalHours are defined as
public class TableTotalHours
{
public int Id {get;set;}
public DateTime Date{get;set;}
public double TotalHours{get;set;}
}
public class TablePricePerHours
{
public int Id{get;set;}
public double Price{get;set;}
public double Tax{get;set;}
public DateTime Date{get;set;}
}
Now you can get the required result with Linq as
var result = tableTotalHours.Select(x=> new
{
Date=x.Date,
TotalHours=x.TotalHours,
CalculatedPrice = tablePricePerHours.Where(c=> (x.Date - c.Date).Ticks>0)
.OrderBy(c=> (x.Date - c.Date).Ticks)
.First()
.Price * x.TotalHours
});
Output
You can find the same here
YES! :) 1074.50 is a correct number! Thank you very much for the answer, I am glad that there are still people who want to help inexperienced programmers! But tell me should I also transfer my tables to List?? and can you please write this "result = tableTotalHours.Select(x=>" on normal c# and not on Linq, so I can understand better.
– s.s.
Jan 1 at 5:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992578%2fcombining-data-from-two-tables-in-a-database-to-get-total-sum-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I have tried to mock your scenario with List.
var tablePricePerHours = new List<TablePricePerHours>
{
new TablePricePerHours{ Price = 10, Tax = 19, Date = new DateTime(2018,1,5)},
new TablePricePerHours{ Price = 10.5, Tax = 21.5, Date = new DateTime(2018,4,22)},
new TablePricePerHours{ Price = 11, Tax = 22.5, Date = new DateTime(2018,6,19)},
new TablePricePerHours{ Price = 11.5, Tax = 23, Date = new DateTime(2018,10,30)},
new TablePricePerHours{ Price = 12, Tax = 23.2, Date = new DateTime(2018,11,1)}
};
var tableTotalHours = new List<TableTotalHours>
{
new TableTotalHours{ Date = new DateTime(2018,02,15), TotalHours = 5},
new TableTotalHours{ Date = new DateTime(2018,02,19), TotalHours =10},
new TableTotalHours{ Date = new DateTime(2018,02,25), TotalHours = 8},
new TableTotalHours{ Date = new DateTime(2018,03,29), TotalHours = 7.5},
new TableTotalHours{ Date = new DateTime(2018,07,05), TotalHours = 9},
new TableTotalHours{ Date = new DateTime(2018,07,06), TotalHours = 1.5},
new TableTotalHours{ Date = new DateTime(2018,07,07), TotalHours = 12},
new TableTotalHours{ Date = new DateTime(2018,10,22), TotalHours = 10},
new TableTotalHours{ Date = new DateTime(2018,10,22), TotalHours = 10},
new TableTotalHours{ Date = new DateTime(2018,11,1), TotalHours = 8},
new TableTotalHours{ Date = new DateTime(2018,12,21), TotalHours = 8.5},
new TableTotalHours{ Date = new DateTime(2018,12,22), TotalHours = 9},
};
Where TablePricePerHours and TableTotalHours are defined as
public class TableTotalHours
{
public int Id {get;set;}
public DateTime Date{get;set;}
public double TotalHours{get;set;}
}
public class TablePricePerHours
{
public int Id{get;set;}
public double Price{get;set;}
public double Tax{get;set;}
public DateTime Date{get;set;}
}
Now you can get the required result with Linq as
var result = tableTotalHours.Select(x=> new
{
Date=x.Date,
TotalHours=x.TotalHours,
CalculatedPrice = tablePricePerHours.Where(c=> (x.Date - c.Date).Ticks>0)
.OrderBy(c=> (x.Date - c.Date).Ticks)
.First()
.Price * x.TotalHours
});
Output
You can find the same here
YES! :) 1074.50 is a correct number! Thank you very much for the answer, I am glad that there are still people who want to help inexperienced programmers! But tell me should I also transfer my tables to List?? and can you please write this "result = tableTotalHours.Select(x=>" on normal c# and not on Linq, so I can understand better.
– s.s.
Jan 1 at 5:18
add a comment |
I have tried to mock your scenario with List.
var tablePricePerHours = new List<TablePricePerHours>
{
new TablePricePerHours{ Price = 10, Tax = 19, Date = new DateTime(2018,1,5)},
new TablePricePerHours{ Price = 10.5, Tax = 21.5, Date = new DateTime(2018,4,22)},
new TablePricePerHours{ Price = 11, Tax = 22.5, Date = new DateTime(2018,6,19)},
new TablePricePerHours{ Price = 11.5, Tax = 23, Date = new DateTime(2018,10,30)},
new TablePricePerHours{ Price = 12, Tax = 23.2, Date = new DateTime(2018,11,1)}
};
var tableTotalHours = new List<TableTotalHours>
{
new TableTotalHours{ Date = new DateTime(2018,02,15), TotalHours = 5},
new TableTotalHours{ Date = new DateTime(2018,02,19), TotalHours =10},
new TableTotalHours{ Date = new DateTime(2018,02,25), TotalHours = 8},
new TableTotalHours{ Date = new DateTime(2018,03,29), TotalHours = 7.5},
new TableTotalHours{ Date = new DateTime(2018,07,05), TotalHours = 9},
new TableTotalHours{ Date = new DateTime(2018,07,06), TotalHours = 1.5},
new TableTotalHours{ Date = new DateTime(2018,07,07), TotalHours = 12},
new TableTotalHours{ Date = new DateTime(2018,10,22), TotalHours = 10},
new TableTotalHours{ Date = new DateTime(2018,10,22), TotalHours = 10},
new TableTotalHours{ Date = new DateTime(2018,11,1), TotalHours = 8},
new TableTotalHours{ Date = new DateTime(2018,12,21), TotalHours = 8.5},
new TableTotalHours{ Date = new DateTime(2018,12,22), TotalHours = 9},
};
Where TablePricePerHours and TableTotalHours are defined as
public class TableTotalHours
{
public int Id {get;set;}
public DateTime Date{get;set;}
public double TotalHours{get;set;}
}
public class TablePricePerHours
{
public int Id{get;set;}
public double Price{get;set;}
public double Tax{get;set;}
public DateTime Date{get;set;}
}
Now you can get the required result with Linq as
var result = tableTotalHours.Select(x=> new
{
Date=x.Date,
TotalHours=x.TotalHours,
CalculatedPrice = tablePricePerHours.Where(c=> (x.Date - c.Date).Ticks>0)
.OrderBy(c=> (x.Date - c.Date).Ticks)
.First()
.Price * x.TotalHours
});
Output
You can find the same here
YES! :) 1074.50 is a correct number! Thank you very much for the answer, I am glad that there are still people who want to help inexperienced programmers! But tell me should I also transfer my tables to List?? and can you please write this "result = tableTotalHours.Select(x=>" on normal c# and not on Linq, so I can understand better.
– s.s.
Jan 1 at 5:18
add a comment |
I have tried to mock your scenario with List.
var tablePricePerHours = new List<TablePricePerHours>
{
new TablePricePerHours{ Price = 10, Tax = 19, Date = new DateTime(2018,1,5)},
new TablePricePerHours{ Price = 10.5, Tax = 21.5, Date = new DateTime(2018,4,22)},
new TablePricePerHours{ Price = 11, Tax = 22.5, Date = new DateTime(2018,6,19)},
new TablePricePerHours{ Price = 11.5, Tax = 23, Date = new DateTime(2018,10,30)},
new TablePricePerHours{ Price = 12, Tax = 23.2, Date = new DateTime(2018,11,1)}
};
var tableTotalHours = new List<TableTotalHours>
{
new TableTotalHours{ Date = new DateTime(2018,02,15), TotalHours = 5},
new TableTotalHours{ Date = new DateTime(2018,02,19), TotalHours =10},
new TableTotalHours{ Date = new DateTime(2018,02,25), TotalHours = 8},
new TableTotalHours{ Date = new DateTime(2018,03,29), TotalHours = 7.5},
new TableTotalHours{ Date = new DateTime(2018,07,05), TotalHours = 9},
new TableTotalHours{ Date = new DateTime(2018,07,06), TotalHours = 1.5},
new TableTotalHours{ Date = new DateTime(2018,07,07), TotalHours = 12},
new TableTotalHours{ Date = new DateTime(2018,10,22), TotalHours = 10},
new TableTotalHours{ Date = new DateTime(2018,10,22), TotalHours = 10},
new TableTotalHours{ Date = new DateTime(2018,11,1), TotalHours = 8},
new TableTotalHours{ Date = new DateTime(2018,12,21), TotalHours = 8.5},
new TableTotalHours{ Date = new DateTime(2018,12,22), TotalHours = 9},
};
Where TablePricePerHours and TableTotalHours are defined as
public class TableTotalHours
{
public int Id {get;set;}
public DateTime Date{get;set;}
public double TotalHours{get;set;}
}
public class TablePricePerHours
{
public int Id{get;set;}
public double Price{get;set;}
public double Tax{get;set;}
public DateTime Date{get;set;}
}
Now you can get the required result with Linq as
var result = tableTotalHours.Select(x=> new
{
Date=x.Date,
TotalHours=x.TotalHours,
CalculatedPrice = tablePricePerHours.Where(c=> (x.Date - c.Date).Ticks>0)
.OrderBy(c=> (x.Date - c.Date).Ticks)
.First()
.Price * x.TotalHours
});
Output
You can find the same here
I have tried to mock your scenario with List.
var tablePricePerHours = new List<TablePricePerHours>
{
new TablePricePerHours{ Price = 10, Tax = 19, Date = new DateTime(2018,1,5)},
new TablePricePerHours{ Price = 10.5, Tax = 21.5, Date = new DateTime(2018,4,22)},
new TablePricePerHours{ Price = 11, Tax = 22.5, Date = new DateTime(2018,6,19)},
new TablePricePerHours{ Price = 11.5, Tax = 23, Date = new DateTime(2018,10,30)},
new TablePricePerHours{ Price = 12, Tax = 23.2, Date = new DateTime(2018,11,1)}
};
var tableTotalHours = new List<TableTotalHours>
{
new TableTotalHours{ Date = new DateTime(2018,02,15), TotalHours = 5},
new TableTotalHours{ Date = new DateTime(2018,02,19), TotalHours =10},
new TableTotalHours{ Date = new DateTime(2018,02,25), TotalHours = 8},
new TableTotalHours{ Date = new DateTime(2018,03,29), TotalHours = 7.5},
new TableTotalHours{ Date = new DateTime(2018,07,05), TotalHours = 9},
new TableTotalHours{ Date = new DateTime(2018,07,06), TotalHours = 1.5},
new TableTotalHours{ Date = new DateTime(2018,07,07), TotalHours = 12},
new TableTotalHours{ Date = new DateTime(2018,10,22), TotalHours = 10},
new TableTotalHours{ Date = new DateTime(2018,10,22), TotalHours = 10},
new TableTotalHours{ Date = new DateTime(2018,11,1), TotalHours = 8},
new TableTotalHours{ Date = new DateTime(2018,12,21), TotalHours = 8.5},
new TableTotalHours{ Date = new DateTime(2018,12,22), TotalHours = 9},
};
Where TablePricePerHours and TableTotalHours are defined as
public class TableTotalHours
{
public int Id {get;set;}
public DateTime Date{get;set;}
public double TotalHours{get;set;}
}
public class TablePricePerHours
{
public int Id{get;set;}
public double Price{get;set;}
public double Tax{get;set;}
public DateTime Date{get;set;}
}
Now you can get the required result with Linq as
var result = tableTotalHours.Select(x=> new
{
Date=x.Date,
TotalHours=x.TotalHours,
CalculatedPrice = tablePricePerHours.Where(c=> (x.Date - c.Date).Ticks>0)
.OrderBy(c=> (x.Date - c.Date).Ticks)
.First()
.Price * x.TotalHours
});
Output
You can find the same here
answered Jan 1 at 4:51
Anu ViswanAnu Viswan
5,4092525
5,4092525
YES! :) 1074.50 is a correct number! Thank you very much for the answer, I am glad that there are still people who want to help inexperienced programmers! But tell me should I also transfer my tables to List?? and can you please write this "result = tableTotalHours.Select(x=>" on normal c# and not on Linq, so I can understand better.
– s.s.
Jan 1 at 5:18
add a comment |
YES! :) 1074.50 is a correct number! Thank you very much for the answer, I am glad that there are still people who want to help inexperienced programmers! But tell me should I also transfer my tables to List?? and can you please write this "result = tableTotalHours.Select(x=>" on normal c# and not on Linq, so I can understand better.
– s.s.
Jan 1 at 5:18
YES! :) 1074.50 is a correct number! Thank you very much for the answer, I am glad that there are still people who want to help inexperienced programmers! But tell me should I also transfer my tables to List?? and can you please write this "result = tableTotalHours.Select(x=>" on normal c# and not on Linq, so I can understand better.
– s.s.
Jan 1 at 5:18
YES! :) 1074.50 is a correct number! Thank you very much for the answer, I am glad that there are still people who want to help inexperienced programmers! But tell me should I also transfer my tables to List?? and can you please write this "result = tableTotalHours.Select(x=>" on normal c# and not on Linq, so I can understand better.
– s.s.
Jan 1 at 5:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992578%2fcombining-data-from-two-tables-in-a-database-to-get-total-sum-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
could you show sample output ?
– Anu Viswan
Jan 1 at 2:20
It appears that no one can work over midnight, so the problem becomes one of finding the applicable hourly rate for a given date. Is the intent that the hourly rate is the one for: (1) the date in question, if it exists, or (B) the latest priced date prior to the date in question? Since you haven't explained how to calculate the total sum it's a bit of a challenge for us to help you. Aside: You convert numeric values to strings to doubles to strings for display in labels? Tip: Reasons not to use images are here.
– HABO
Jan 1 at 2:22
@HABO If you do not want to help you do not have to, and the question was not for that strings,doubles,strings, code is in the test phase. And any help even if is not in code is welcome, and if I had experience, I would not ask the question here.
– s.s.
Jan 1 at 2:45
@Anu Viswan For example, on 07.05.2018 the total hours for this day was 9h, in that period (from tb_PricePerH) the price of the hour was 11, because it started 06.19.2018. So on this day the profit was 99.
– s.s.
Jan 1 at 2:46
You may find this answer to be helpful.
– HABO
Jan 1 at 4:42