I have data being returned in a DataTable that looks like:
Department | EmployeeName
1 | Employee1
1 | Employee1
2 | Employee2
3 | Employee3
3 | Employee3
3 | Employee3
I'm trying to get only distinct rows and put it into a collection, like so:
IEnumerable<Department> departments = dt.AsEnumerable().Select(row =>
new Department
{
DepartmentID = Int32.Parse(row["DepartmentID"].ToString()),
Employee = new Employee { EmployeeName = row["EmployeeName"].ToString() }
}).Distinct();
It should only return 3 rows, but instead it's returning all 6.
What am I doing wrong here?
Not sure but one of the following may work for you ..........
just do this
var distrows= table1.AsEnumerable().Distinct();
will do your task..........than create collection of department form the distrow..
IEnumerable<Department> departments = (from DataRow dRow in distrows
new Department
{
DepartmentID = Int32.Parse(row["DepartmentID"].ToString()),
Employee = new Employee { EmployeeName = row["EmployeeName"].ToString() }
});
OR
var distinctRows = (from DataRow dRow in dTable.Rows
select new {col1=dRow["dataColumn1"],col2=dRow["dataColumn2"]}).Distinct();
IEnumerable<Department> departments = (from DataRow dRow in distrows
new Department
{
DepartmentID = Int32.Parse(distinctRows.col1),
Employee = new Employee { EmployeeName = distinctRows.col2.ToString() }
});
Class/Schema
public class abc
{
public int id;
}
DataTable and Records
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("id", Type.GetType("System.Int32"));
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr["id"] = 1;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["id"] = 1;
dt.Rows.Add(dr);
Query
var qa = dt.AsEnumerable()
.Select(row => new
{
Id = row.Field<int>("id"),
})
.Distinct();
what i think the problem here is that you are creating different objects with have no implementation for comparing the equality based on some propertiese. so the Distinct() method compares the refference of the objects and hence concludes that all the objects are different because they point to different reffrences.
what you shoud do instead is try parsing the information in an object that implements IEqualityComparer and then use .Select(<IEqualityComparer>).Distinct()
Related
I am not using Entity Framework.
There are 2 Datatables, each with different no. of columns. There is one common column - ItemId
If it was Union, I would have used 'Merge', but don't know how to do a Union All for these 2 DataTables
public static void Main()
{
//First DataTable
DataTable dt1 = new DataTable();
dt1.Columns.Add("ItemId");
dt1.Columns.Add("Name");
dt1.Columns.Add("Color");
DataRow dr = dt1.NewRow();
dr["ItemId"] = "1";
dr["Name"] = "Name1";
dr["Color"] = "Color1";
dt1.Rows.Add(dr);
dr = dt1.NewRow();
dr["ItemId"] = "2";
dr["Name"] = "Name2";
dr["Color"] = "Color2";
dt1.Rows.Add(dr);
//Second DataTable
DataTable dt2 = new DataTable();
dt2.Columns.Add("ItemId");
dt2.Columns.Add("Name");
dt2.Columns.Add("Price");
DataRow dr2 = dt2.NewRow();
dr2["ItemId"] = "1";
dr2["Name"] = "Name1";
dr2["Price"] = "100";
dt2.Rows.Add(dr2);
dr2 = dt2.NewRow();
dr2["ItemId"] = "2";
dr2["Name"] = "Name3";
dr2["Price"] = "200";
dt2.Rows.Add(dr2);
}
Expected Output
ItemId Name Color Price
1 Name1 Color1
2 Name2 Color2
1 Name1 100
2 Name3 200
You can use DataTable.Merge()
If you have primary key in both the table then it will perform merge on primary key else it will directly append all the records.
In youe case make ItemID as primary key.
using System.Linq.Expressions;
......
var result1 = from row1 in dt1.AsEnumerable()
select new { Name = row1.Field<String>("Name"), Color = row1.Field<String>("Color"), Price = "" };
var result2 = from row1 in dt2.AsEnumerable()
select new { Name = row1.Field<String>("Name"), Color = "", Price = row1.Field<String>("Price") };
var res = result1.Concat(result2);
foreach (var item in res)
Console.WriteLine("{0} - {1} - {2}", item.Name, item.Color, item.Price);
Even though this question is years old, for anyone looking for another way to do this:
public static DataTable MergeTables(DataTable dt1, DataTable dt2)
{
DataTable dt3 = dt1.Clone();
foreach (DataColumn col in dt2.Columns)
{
string strColumnName = col.ColumnName;
int intColNum = 1;
while (dt3.Columns.Contains(strColumnName))
{
strColumnName = string.Format("{0}_{1}", col.ColumnName, ++intColNum);
}
dt3.Columns.Add(strColumnName, col.DataType);
}
var Mergered = dt1.AsEnumerable().Zip(dt2.AsEnumerable(), (r1, r2) => r1.ItemArray.Concat(r2.ItemArray).ToArray());
foreach (object[] rowFields in Mergered)
dt3.Rows.Add(rowFields);
return dt3;
}
I have the following LINQ query:
var timesheets = from timesheet in entities.Timesheets
join timesheetTask in entities.Timesheet_Task on timesheet.Id equals timesheetTask.Timesheet_Id
join task in entities.Tasks on timesheetTask.Task_Id equals task.Id
join project in entities.Projects on task.Project_Id equals project.Id
join department in entities.Departments on project.Department_Id equals department.Id
where timesheet.Employee_Id == employeeId
select new
{
date = timesheet.Date,
taskName = task.Name,
projectName = project.Name,
projectDesc = project.Description,
departmentName = department.Name,
taskEstimatedHours = task.Estimated_Hours,
timesheetHours = timesheetTask.Hours
};
How can I put these results into a DataTable which I can then bind to a DataGridView control?
This is what I'm currently doing:
table.Columns.Add("date");
table.Columns.Add("taskName");
table.Columns.Add("projectName");
table.Columns.Add("projectDesc");
table.Columns.Add("departmentName");
table.Columns.Add("taskEstimatedHours");
table.Columns.Add("timesheetHours");
foreach (var item in timesheets)
{
table.Rows.Add(item.date, item.taskName, item.projectName,
item.projectDesc, item.departmentName, item.taskEstimatedHours,
item.timesheetHours);
}
}
Update: Here is my updated code:
DataTable table = new DataTable();
using (PTMS_DataEntities entities = new PTMS_DataEntities())
{
var timesheets = from timesheet in entities.Timesheets
join timesheetTask in entities.Timesheet_Task on timesheet.Id equals timesheetTask.Timesheet_Id
join task in entities.Tasks on timesheetTask.Task_Id equals task.Id
join project in entities.Projects on task.Project_Id equals project.Id
join department in entities.Departments on project.Department_Id equals department.Id
where timesheet.Employee_Id == employeeId
select new
{
date = timesheet.Date,
taskName = task.Name,
projectName = project.Name,
projectDesc = project.Description,
departmentName = department.Name,
taskEstimatedHours = task.Estimated_Hours,
timesheetHours = timesheetTask.Hours
};
table.Columns.Add("date", typeof(DateTime));
table.Columns.Add("taskName", typeof(string));
table.Columns.Add("projectName", typeof(string));
table.Columns.Add("projectDesc", typeof(string));
table.Columns.Add("departmentName", typeof(string));
table.Columns.Add("taskEstimatedHours", typeof(int));
table.Columns.Add("timesheetHours", typeof(int));
List<DataRow> list = new List<DataRow>();
foreach (var item in timesheets)
{
//table.Rows.Add(item.date, item.taskName, item.projectName,
// item.projectDesc, item.departmentName, item.taskEstimatedHours,
// item.timesheetHours);
var row = table.NewRow();
row.SetField<DateTime>("date", item.date);
row.SetField<string>("taskName", item.taskName);
row.SetField<string>("projectName", item.projectName);
row.SetField<string>("projectDesc", item.projectDesc);
row.SetField<string>("departmentName", item.departmentName);
row.SetField<int>("taskEstimatedHours", item.taskEstimatedHours);
row.SetField<int>("timesheetHours", item.timesheetHours);
list.Add(row);
}
table = list.CopyToDataTable();
}
Here is the SQL query I tested in SSMS (which should be the equivalent of the LINQ query):
SELECT dbo.Department.Name, dbo.Task.Name AS Expr1, dbo.Task.Estimated_Hours, dbo.Timesheet.Date, dbo.Project.Name AS Expr2, dbo.Project.Description,
dbo.Timesheet_Task.Date AS Expr3
FROM dbo.Department INNER JOIN
dbo.Project ON dbo.Department.Id = dbo.Project.Department_Id INNER JOIN
dbo.Task ON dbo.Project.Id = dbo.Task.Project_Id INNER JOIN
dbo.Timesheet_Task ON dbo.Task.Id = dbo.Timesheet_Task.Task_Id INNER JOIN
dbo.Timesheet ON dbo.Timesheet_Task.Timesheet_Id = dbo.Timesheet.Id
If you really want to populate DataTable:
// your query
var timesheets = ...
// design table first
DataTable table = new DataTable();
table.Columns.Add(new DataColumn
{
ColumnName = "TaskName",
DataType = typeof(String);
});
...
List<DataRow> list = new List<DataRow>();
foreach (var t in timesheets)
{
var row = table.NewRow();
row.SetField<string>("TaskName", t.taskName); // extension method from System.Data.DataSetExtensions.dll
...
list.Add(row);
}
DataTable table = list.CopyToDataTable(); // extension method too
Or more LINQ way:
timesheets
.Select(t =>
{
var row = table.NewRow();
...
return row;
})
.CopyToDataTable();
Or in same query syntax. Implement a method:
static DataRow NewRow(DataRow row, string taskName, ....)
{
...
}
Then query itself:
(from ...
where ...
select NewRow(table.NewRow(), task.Name, ...)
).CopyToDataTable();
Call .ToList().
The resulting List<T> can also be bound to a DataGridView, and is easier to work with than a DataTable.
I'm using FastMember for this purpose. It uses IL instead of reflection (much faster) to iterate over all of the property and field values automatically. Code sample from the site:
IEnumerable<SomeType> data = ...
var table = new DataTable();
using(var reader = ObjectReader.Create(data))
{
table.Load(reader);
}
I have a DataSet with 2 DataTable's. Each DataTable contains a column called "cost".
I want to calculate the sum of all costs for the 2 tables in a table called Result table, like the example below. How can I do that?
Table 1
Name | cost
balan | 6
gt | 5
Table 2
Name | cost
balan | 2
gt | 8
Result table
Name | cost
balan | 8
gt | 12
This is a way to do it:
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
DataTable results = new DataTable();
dt1.Columns.Add("Name");
dt1.Columns.Add("cost", typeof(int));
dt2.Columns.Add("Name");
dt2.Columns.Add("cost", typeof(int));
results.Columns.Add("Name");
results.Columns.Add("cost", typeof(int));
dt1.Rows.Add("balan", 6);
dt2.Rows.Add("balan", 2);
dt1.Rows.Add("gt", 5);
dt2.Rows.Add("gt", 8);
foreach (DataRow dr1 in dt1.Rows)
{
results.Rows
.Add(
dr1["Name"],
(int)dr1["cost"] + (int)dt2.Select(String.Format("Name='{0}'", dr1["name"]))[0]["cost"]
);
}
to get the result, you can do something like
var table1 = yourDataSet.Tables["Table 1"];
var table2 = yourDataSet.Tables["Table 2"];
var results = table1.AsEnumerable().Select(t1 => new {
name = t1.Field<string>("Name"),
cost = t1.Field<int>("cost")
})
.Concat(
table2.AsEnumerable().Select(t2 => new {
name = t2.Field<string>("Name"),
cost = t2.Field<int>("cost")
})
)
.GroupBy(m => m.name)
.Select(g => new {
name = g.Key,
cost = g.Sum(x => x.cost)
});
this won't give you a dataTable, but an IEnumerable. To transform an IEnumerable to a dataTable, see for example here
or easier, if table1 and table2 have same rows
var table1 = yourDataSet.Tables["Table 1"];
var table2 = yourDataSet.Tables["Table 2"];
var results = new DataTable();
results.Columns.Add("Name");
results.Columns.Add("cost", typeof(int));
table1.AsEnumerable().Concat(table2.AsEnumerable())
.GroupBy(m => m.Field<string>("Name"))
.Select(g => results.Rows.Add(g.Key, g.Sum(x => x.Field<int>("cost"))));
I have 3 Data tables populated with a dataset and table adapters/ binding sources and i need to run a Join query or find another way to get specific data. (the dataset contains each table listed)
Tables:
Product Table:
Prod_ID Name
1 tv
2 couch
Consumer Table:
Con_Id Name City
----------------------
1 Gray New York
2 Joe Chicago
3 Steve Madison
Transactions Table
Tran_Id Con_ID Prod_ID Price
-------------------------------------
1 2 1 900
2 1 2 300
Given a product name i need to populate a table with each distinct city and how much that product has sold for in that city (add all prices for that product to any consumer in a given city)
I am really stumped and cant find a way. (i have tried alot)
Please help and thank you!
Nudiers approach so far:
DataRelation relation = null;
DataColumn table1Column = null;
DataColumn table2Column = null;
DataColumn table3Column = null;
table1Column = tlobergeDataSet.Tb_Product.Columns[0];
table2Column = tlobergeDataSet.Tb_Transactions.Columns[3];
table3Column = tlobergeDataSet.Tb_Consumer.Columns[0];
relation = new DataRelation("relation", table1Column, table2Column);
tlobergeDataSet.Relations.Add(relation);
public DataTable MergeTables(DataTable dtFirst, DataTable dtSecond, string CommonColumn)
{
DataTable dtResults = dtFirst.Clone();
int count = 0;
for (int i = 0; i < dtSecond.Columns.Count; i++)
{
if (!dtFirst.Columns.Contains(dtSecond.Columns[i].ColumnName))
{
dtResults.Columns.Add(dtSecond.Columns[i].ColumnName, dtSecond.Columns[i].DataType);
count++;
}
}
DataColumn[] columns = new DataColumn[count];
int j = 0;
for (int i = 0; i < dtSecond.Columns.Count; i++)
{
if (!dtFirst.Columns.Contains(dtSecond.Columns[i].ColumnName))
{
columns[j++] = new DataColumn(dtSecond.Columns[i].ColumnName, dtSecond.Columns[i].DataType);
}
}
dtResults.BeginLoadData();
foreach (DataRow dr in dtFirst.Rows)
{
dtResults.Rows.Add(dr.ItemArray);
}
foreach (DataRow dr in dtSecond.Rows)
{
foreach (DataRow dr1 in dtResults.Rows)
{
if (dr1[CommonColumn].ToString().Equals(dr[CommonColumn].ToString()))
{
foreach (DataColumn c in columns)
{
dr1[c.ColumnName] = dr[c.ColumnName];
}
}
}
}
dtResults.EndLoadData();
return dtResults;
}
try with this.
DataRelation relation = null;
DataColumn table1Column = null;
DataColumn table2Column = null;
//retrieve column
table1Column = ds.Tables("Table1").Columns(0);
table2Column = ds.Tables("table2").Columns(0);
//relating tables
relation = new DataRelation("relation", table1Column, table2Column);
//assign relation to dataset
ds.Relations.Add(relation);
You can only relate two tables in a DataRelation object and to access the data from
the dataset is straightforward because the data has been already related.
DataRelation relation = null;
DataColumn table1Column = null;
DataColumn table2Column = null;
DataColumn table3Column = null;
table1Column = tlobergeDataSet.Tb_Product.Columns[0];
table2Column = tlobergeDataSet.Tb_Transactions.Columns[2];
table2Column1 = tlobergeDataSet.Tb_Transactions.Columns[1];
table3Column = tlobergeDataSet.Tb_Consumer.Columns[0];
relation = new DataRelation("relation", table1Column, table2Column);
tlobergeDataSet.Relations.Add(relation);
relation = new DataRelation("relation1", table3Column , table2Column1);
tlobergeDataSet.Relations.Add(relation);
In LINQ, you can join the tables to find the data you want with syntax like so:
from a in keyTable
join b in anotherTable on a.Key equals b.Key
join c in aThirdTable on a.Key equals c.Key
select new
{
// Anonymous Object Properties using identifier a, b, and c to get data
};
You should be able to take that snippet and generate a linq query that will generate an anonymous object containing the specific data representation that you need.
I have this requirement that the user request one or more partnumbers.And i need to display all the result in one Data Set
Ex: Request SerialNumbers with
PartNumber Required Quantity
A 2
B 1
C 3
Code:
//Loop Request
foreach (DataRow a in request.Rows)
{
//select top(Quantity) SerialNumber,PartNumber where PartNumber=#PartNumber
var dt = new DataTable();
}
//1. PartNumber A
SerialNumber Partnumber
1 A
2 A
+
//2. PartNumber B
SerialNumber Partnumber
1 B
//3. PartNumber C
SerialNumber Partnumber
1 C
2 C
3 C
//Load to DataSet
And the result will be something like:
SerialNumber Partnumber
1 A
2 A
1 B
1 C
2 C
3 C
Thanks in Regards
I am not sure what two tables you wish to join here and on what key but regadless of that, the simplest way will be to use LINQ to Dataset. For example,
var query = from a in table1
join
b in table2
on
a.FkColumn = b.PKColumn select a,b;
Please check below logic:
DataTable myDataTable = new DataTable();
DataColumn PartNumber = new DataColumn("PartNumber");
DataColumn RequiredQuantity = new DataColumn("RequiredQuantity");
myDataTable.Columns.Add(PartNumber);
myDataTable.Columns.Add(RequiredQuantity);
DataRow dataRowPN1 = myDataTable.NewRow();
DataRow dataRowPN2 = myDataTable.NewRow();
DataRow dataRowPN3 = myDataTable.NewRow();
dataRowPN1["PartNumber"] = "A";
dataRowPN2["PartNumber"] = "B";
dataRowPN3["PartNumber"] = "C";
dataRowPN1["RequiredQuantity"] = "2";
dataRowPN2["RequiredQuantity"] = "1";
dataRowPN3["RequiredQuantity"] = "3";
myDataTable.Rows.Add(dataRowPN1);
myDataTable.Rows.Add(dataRowPN2);
myDataTable.Rows.Add(dataRowPN3);
int i = myDataTable.Rows.Count;
DataTable joinDataTable = new DataTable();
DataColumn SerialNumber = new DataColumn("SerialNumber");
DataColumn JoinPartNumber = new DataColumn("PartNumber");
joinDataTable.Columns.Add(SerialNumber);
joinDataTable.Columns.Add(JoinPartNumber);
foreach (DataRow dr in myDataTable.Rows)
{
for (int count = 1; count <= Convert.ToInt16(dr["RequiredQuantity"]); count++)
{
DataRow joindataRow = joinDataTable.NewRow();
joindataRow["SerialNumber"] = count.ToString().Trim();
joindataRow["PartNumber"] = dr["PartNumber"].ToString().Trim();
joinDataTable.Rows.Add(joindataRow);
}
}
Response.Write(joinDataTable.Rows.Count);