combining data from two tables in a database to get total sum (C#)












-3















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.
enter image description here



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();
}









share|improve this question























  • 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
















-3















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.
enter image description here



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();
}









share|improve this question























  • 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














-3












-3








-3








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.
enter image description here



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();
}









share|improve this question














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.
enter image description here



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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












1 Answer
1






active

oldest

votes


















1














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



enter image description here



You can find the same here






share|improve this answer
























  • 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













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
});


}
});














draft saved

draft discarded


















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









1














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



enter image description here



You can find the same here






share|improve this answer
























  • 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


















1














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



enter image description here



You can find the same here






share|improve this answer
























  • 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
















1












1








1







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



enter image description here



You can find the same here






share|improve this answer













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



enter image description here



You can find the same here







share|improve this answer












share|improve this answer



share|improve this answer










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





















  • 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






















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas