Doing Calculations of a group - c#

I have a Visual Studio Program where the user enters specific data of production for that day. Example:
Date | Part Number | Mold Num | Machine Num | Total Parts |Cycle
2/12/2016 | 1185-5B8 | 6580 | 12 | 220 | 56
2/12/2016 | 2249300 | 7797 | 36 | 600 | 13
2/12/2016 | 146865 | 5096789 | 56 | 500 | 15
2/16/2016 | 123456 | 7787 | 54 | 300 | 34
2/16/2016 | 123456 | 787 | 54 | 360 | 36
2/16/2016 | 123456 | 777 | 54 | 500 | 46
2/16/2016 | 123456 | 87 | 54 | 400 | 44
I'm trying to have people enter production data and then manipulate it in order to get the machine usage (MU) (Equation: Total Parts/(3600/Cycle). I want to get these things for each day and for a specific month when I call it. I looked at what others have done but they are just counting how many are in a specific date but I need to multiply, and divide other columns together in two different tables with the same dates in order to get what I need.
Example output:
2/12/2016: MU = 41.12%
2/16/2016: MU = 24.79%
February: MU = 32.96%
EDIT:
I would like to do something in the lines of this example but I do not know how to implement it to an already existing DataSet.
Grouping and Sum Datatable by two fields with different Where conditions
or this example:
Calculating percentage of a groups from datatable

Related

C# winform & SQL Create line chart "Query and binding"

I have a table in SQLserver with some values as following
+----------+------+-------+-------+-----+--------+
| MonTime | Temp | Steam | Water | Air | Vacuum |
+----------+------+-------+-------+-----+--------+
| 16:08:08 | 0 | 38 | 57 | 76 | 95 |
| 16:09:08 | 9 | 28 | 47 | 66 | 85 |
| 16:10:08 | 18 | 37 | 56 | 76 | 95 |
| 16:11:08 | 9 | 28 | 47 | 66 | 85 |
| 16:12:08 | 9 | 28 | 47 | 66 | 85 |
| 16:13:08 | 18 | 37 | 56 | 75 | 94 |
| 16:14:08 | 9 | 28 | 47 | 66 | 85 |
+----------+------+-------+-------+-----+--------+
I need to create a line chart in winform application. where x is timeline "MonTime" and y is values and a line for each col "Temp, Steam, Water, etc"
I don't know how to accomplish this. Any help??
Thanks for your support. The problem was with the MonTime datatype "Time(0)" this datatype is not supported by Chart control . I converted it to datetime in SQL and then set it up as Time in chart control X value type.

Editing C# property value from SQL Server when multiple rows provide answer in SQL

Assume I have a SQL table thus:
[Sample Data]
ID1 --------| ID2 | EditColumn | EditValue
T1017TF | 142 | EyeColor | 1
T1017TF | 142 | EyeColor | 2
T1017TF | 142 | HairColor | F
T1017TF | 142 | WeightClass | A1
T1017TF | 142 | WeightClass | A2
T1017TF | 142 | AcceptablePulse | 1
T1017TF | 142 | AcceptablePulse | 3
From C# assuming I have the EditValue of each EditColumn in a .Net Class Property corresponding to combined keys ID1 AND ID2. So, for Key T1017TF/142, the WeightClass= 'A1'or 'A2' would pass validation, but A3 would not. For a given key combo (T1017TF/142), I must edit for each EditColumn.
What is the best manner in C# or Tranact SQL (up to 2016) to edit repeatedly for each EditColumn? I must be overthinking this..
Thank you ahead of time

How to join Join two DataRows into a single DataRow through Column Name?

I am trying to figure out how to join two respective datarows into single datarow in dataset through Department column Name.
In provided dataset output i want to join Gastroenterology and Medical Gastroen(two datrows) through column name to single datarow (similar to Required Final dataset Output with Merged Rows).
Need Your ideas/help how it can be accomplished in asp.net and/or C#.
DataSet Output
Department Male Visit Female Visit Total Count
---------- ---------- ------------ -----------
Endocrinology 10 20 30
Gastroenterology 15 25 40
General Medicine 25 05 30
Medical Gastroen 30 20 50
Required Final Dataset Output with Merged Rows
Department Male Visit Female Visit Total Count
---------- ---------- ------------ -----------
Endocrinology 10 20 30
Gastroenterology 45 45 90
General Medicine 25 05 30
I think you must use joining for this in your database query. that would be better.
Table A
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Table B.
+-----+---------------------+-------------+--------+
| OID | DATE | ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |
+-----+---------------------+-------------+--------+
SQL QUERY
SQL> SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
INNER JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
Resulted Table:
+----+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+----+----------+--------+---------------------+
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
+----+----------+--------+---------------------+
I hope it will help you.
You can Do something like this.
DataTable _dataTable = new DataTable();
DataRow _dataRow1 = null;
_dataTable.TableName = "Products";
_dataTable.Columns.Add("ID",typeof(int));
_dataTable.Columns[0].AutoIncrementSeed = 1;
_dataTable.Columns[0].AutoIncrement = true;
_dataTable.Columns.Add("ProductsName");
_dataTable.Columns.Add("Price");
_dataRow1 = _dataTable.NewRow();
_dataRow1["ProductsName"] = "Sony Laptop";
_dataRow1["Price"] = "15000";
_dataTable.Rows.Add(_dataRow1);
DataRow _dataRow2 = null;
_dataRow2 = _dataTable.NewRow();
_dataTable.Rows.Add(_dataRow2);
_dataRow2["ProductsName"] = "LG Laptop";
_dataRow2["Price"] = "15000";
DataSet _dataSet = new DataSet();
_dataSet.Tables.Add(_dataTable);

Show data from two model with same column name same value in ASP.NET

I'm new to ASP.NET, I have only experiences in C# Windows Form and SQL Server. Now I have starting my new project in ASP.NET MVC and already have database from my customer. It has two model included some same column name like this:
Table 1
---------------------------------
| Location | Item | Model | Tag |
---------------------------------
| 1 | 10 | A5 | 221 |
| 2 | 10 | A6 | 233 |
| 3 | 12 | A8 | 332 |
| 4 | 15 | C1 | 223 |
Table 2
-------------------------------------------------
| Location | Item | Model | Tag | DWeek | DYear |
-------------------------------------------------
| 1 | 10 | A5 | 221 | 01 | 15 |
| 2 | 10 | A6 | 233 | 01 | 15 |
| 3 | 12 | A8 | 332 | 02 | 15 |
| 4 | 15 | C1 | 223 | 03 | 15 |
I just want to show the data which have same Location and Item in one table, I don't know how to query in Entity framework, I have only know how to code by SQL, and I think it seem to be like this:
select
r.Location
,r.Item
,d.Location
,d.Item
,d.DWeek
,d.DYear
,r.Model
,r.[Tag No]
,d.[Tag No]
from Register r, Due d
where r.Location = d.Location
and r.Item = d.Item
My question is how to query like this in Entity framework for show data in a view, and is it possible to update and create two model in the same time?
I am using Lambda Expression
var Query=Register.Join(Due,r=>r.Location,d=>d.Location,(r,d)=>new {r,d}).Where(
X=>X.r.Location = X.d.Location && X.r.Item = X.d.Item).Select(X=>new {
LOCATION_1 =X.r.Location;
ITEM_1=X.r.Item;
LOCATION_2=X.d.Location;
ITEM_2=X.d.Item;
DWEEK=X.d.DWeek;
DTEAR=X.d.DYear;
MODEL=X.r.Model;
TAGNO_1=X.r.Tag_No;
TAGNO_1=X.d.Tag_No;
}).ToList();

Solution for Dividing WorkSheet into Multiple Files with vba/excel/c#

I would like to divide a worksheet into multiple files.
I have a worksheet with about 10,000 rows. there is fancy formatting, conditional formatting, nice colors, and I want to preserve all of these attributes.
I need to divide this worksheet up.
the input would be:
+-------+----+----+----+----+
| Alex | 45 | 6 | 23 | 56 |
| Alex | 61 | 47 | 56 | 56 |
| Liza | 49 | 70 | 34 | 37 |
| Alex | 33 | 30 | 22 | 39 |
| Tommy | | 66 | 62 | 29 |
| Liza | | 38 | 49 | 80 |
| Alex | 23 | 56 | 56 | 39 |
| Liza | 32 | 46 | 40 | 43 |
| Liza | | 90 | 24 | 38 |
| Tommy | 38 | 10 | 52 | 23 |
| Nancy | 35 | 36 | 23 | 25 |
+-------+----+----+----+----+
and the output would be separate files like this (please keep in mind i want to preserve all the fancy formatting, and thus the solution has work directly with excel, and not with just CSV (because csv cannot retain formatting))
end products:
+------+----+----+----+----+
| | | | | |
| Alex | 45 | 6 | 23 | 56 |
| Alex | 61 | 47 | 56 | 56 |
| Alex | 33 | 30 | 22 | 39 |
| Alex | 23 | 56 | 56 | 39 |
+------+----+----+----+----+
and
+------+----+----+----+----+
| | | | | |
| Liza | 49 | 70 | 34 | 37 |
| Liza | | 38 | 49 | 80 |
| Liza | 32 | 46 | 40 | 43 |
| Liza | | 90 | 24 | 38 |
+------+----+----+----+----+
and
+-------+----+----+----+----+
| | | | | |
| Nancy | 35 | 36 | 23 | 25 |
+-------+----+----+----+----+
and
+-------+----+----+----+----+
| | | | | |
| Tommy | | 66 | 62 | 29 |
| Tommy | 38 | 10 | 52 | 23 |
+-------+----+----+----+----+
the solution can be a combination of VBA/.NET. please note that i need multiple files as outputs.
what is the quickest way to get this working? thanks so much for any input!
please note that this is excel 2007 and later
I done this before.
You can use this code:
Option Explicit
Sub getInformations()
Dim varName As String
Application.ScreenUpdating = False
'Replace Tabelle1 with the name of your sheet where the Informations are
Worksheets("Tabelle1").Select
Worksheets("Tabelle1").Copy After:=Sheets("Tabelle1")
Sheets("Tabelle1 (2)").Select
Sheets("Tabelle1 (2)").Name = "Temp"
Do Until Range("A1").Value = vbNullString
varName = Range("A1").Value
Workbooks.Add
'Change the Path where you want to save the File
ActiveWorkbook.SaveAs ("C:\Documents and Settings\vgellhom\Desktop\" & varName & ".xls")
'Change The Name of the Excel Workbopk to the Name of the Workbook with the Names
Workbooks("Data.xls").Activate
Sheets("Temp").Select
varName = Range("A1").Value
Do While True
Cells.Find(What:=varName).Activate
Range(ActiveCell.Row & ":" & ActiveCell.Row).Select
Selection.Copy
Workbooks(varName & ".xls").Activate
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Activate
'Change The Name of the Excel Workbopk to the Name of the Workbook with the Names
Workbooks("Data.xls").Activate
Sheets("Temp").Select
Selection.Delete Shift:=xlUp
If Not Cells.FindNext(After:=ActiveCell) Is Nothing Then
Cells.Find(What:=varName).Activate
Else
Exit Do
End If
Loop
Workbooks(varName & ".xls").Activate
'Change the Path where you want to save the File
Application.DisplayAlerts = False
ActiveWorkbook.Save
Application.DisplayAlerts = True
Workbooks(varName & ".xls").Close
Loop
Application.DisplayAlerts = False
Sheets("Temp").Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Hope that helps you...
Since Excel formatting is usually a big pain in the a**, I would recommend to try a following solution:
Calculate and store all the unique keys.
Create a copy of a file for each key (like file_Alex.xls[x], file_Liza.xls[x] and so on).
Process each file, deleting all non-key rows, thus all key entries are left. Also because you are only deleting entire rows all the formatting in file is retained.
This is very unoptimized, but also extremely simple solution. If it's a one-time job, it should do just fine.

Categories

Resources