I have two datatables and I want to join them in several columns.here is my code
DataTable targetTable = dtl.Clone();
var dt2Columns = dt.Columns.OfType<DataColumn>().Select(dc =>
new DataColumn(dc.ColumnName, dc.DataType, dc.Expression, dc.ColumnMapping));
targetTable.Columns.AddRange(dt2Columns.ToArray());
var rowData =
from row1 in dtl.AsEnumerable()
join row2 in dt.AsEnumerable()
on row1.Field<string>("header")
equals row2.Field<string>("header")
where (row1.Field<string>("digitnumber") != "")
select row1.ItemArray.Concat(row2.ItemArray).ToArray();
foreach (object[] values in rowData)
targetTable.Rows.Add(values);
but it joins only on one condition.I want the code like below:
DataTable targetTable = dtl.Clone();
var dt2Columns = dt.Columns.OfType<DataColumn>().Select(dc =>
new DataColumn(dc.ColumnName, dc.DataType, dc.Expression, dc.ColumnMapping));
targetTable.Columns.AddRange(dt2Columns.ToArray());
var rowData =
from row1 in dtl.AsEnumerable()
join row2 in dt.AsEnumerable()
on row1.Field<string>("header") and row1.Field<string>("digitrow") and row1.Field<string> ("response")
equals row2.Field<string>("header") and row1.Field<string>("question") and and row1.Field<string> ("answer")
where (row1.Field<string>("digitnumber") != "")
select row1.ItemArray.Concat(row2.ItemArray).ToArray();
foreach (object[] values in rowData)
targetTable.Rows.Add(values);
but have some errors on multiple conditions.what's the solution?help me please.
from table1 in dt.TableA
join table2 in dt.Table2 on
new { table1.col1, table1.col2}
equals
new { table2.Column1,table2.Column2}
You can add multiple columns in the paranthesis in the same order for comparison.
Related
Hi basically i wanted to group my datatable by the department column, i want to load the results back into another datatable through looping but it keeps giving me an error, im not sure how to group a datatable with linq and load it into another datatable other than looping
here is my code
DataTable data = new DataTable();
data.Columns.Add("Department");
var query1 = dtClone.AsEnumerable().GroupBy(row => row.Field<string>("department"));
if (query1.Any())
foreach(DataRow dr in query1)//here
{
DataRow newrow = data.Rows.Add();
newrow.SetField("Department", dr.Field<string>("department"));
}
foreach (DataRow row in data.Rows)
MessageBox.Show(row[0].ToString());
another linq query i tried
DataTable data = new DataTable();
data.Columns.Add("Department");
var query1 = from r in dtClone.AsEnumerable()
orderby r.Field<string>("department") ascending
group r by r.Field<string>("department") into r
select r;
if (query1.Any())
foreach(DataRow dr in query1)
{
DataRow newrow = data.Rows.Add();
newrow.SetField("Department", dr.Field<string>("department"));
}
No need to group data in case of getting distinct records (only one column).
var query1 = dtClone.AsEnumerable().Select(row => row.Field<string>("department")).Distinct();
ORDER BY
var query1 = dtClone.AsEnumerable().Select(row => row.Field<string>("department")).Distinct().OrderBy(s => s);
I have two arrays converted from datatables.
I need to join the two arrays on a condition and then databind the final results to a ListView.
The first array has only one column, and the second has several. I need to join them where column txtItemA on searchResults is equal to txtItemNumber on queryResults column.
I'm not sure even if I'm on the right track. Visual Studio is showing a redline under searchresults in this line....
var showResults = from a in searchResults
My code...
ArrayList searchResults = new ArrayList();
foreach (DataRow dataRow in dt2.Rows)
searchResults.Add(string.Join(";", dataRow.ItemArray.Select(item => item.ToString())));
ArrayList queryResults = new ArrayList();
foreach (DataRow dataRow in dt2.Rows)
queryResults.Add(string.Join(";", dataRow.ItemArray.Select(item => item.ToString())));
var showResults = from a in searchResults
join b in queryResults on b.txtItemNumber equals a.txtItemA
select new
{
newseries = searchResults.newseries,
series = searchResults.series
};
ListView1.DataSource = showResults.ToArray();
ListView1.DataBind();
I tried using this code from Gilad Green...
List<string> searchResults = new List<string>();
foreach (DataRow dataRow in dt2.Rows)
searchResults.Add(string.Join(";", dataRow.ItemArray.Select(item => item.ToString())));
List<string> queryResults = new List<string>();
foreach (DataRow dataRow in dt2.Rows)
queryResults.Add(string.Join(";", dataRow.ItemArray.Select(item => item.ToString())));
var showResults = searchResults.Where(item => queryResults.Contains(item.txtItemA)
.Select(item => new {
newseries = item.newseries,
series = item.series
}));
I still cant get this to work..
Errors on item in the line.....Select(item => new {
parameter or local variable cannot have the same name as a method type parameter
and txtItemA
string does contain a definition for txtItemA and no extension method....
Try the following, It uses standard DataRows and I'm guessing the field names based on your sample code.
DataTable dt2 = new DataTable();
var showResults = dt2.Rows.Cast<DataRow>().Join(dt2.Rows.Cast<DataRow>(), a => a.Field<string>("txtItemNumber"), b => b.Field<string>("txtItemA"), (a, b) => new
{
newseries = a.Field<string>("newseries"),
series = a.Field<string>("series")
});
ListView1.DataSource = showResults.ToArray();
ListView1.DataBind();
I have the following LINQ query :
var groupedData = from b in loans.AsEnumerable()
group b by b.Field<int>("loan_code") & b.Field<int>("emp_num")
into f
select f.CopyToDataTable();
I want to select f and in addition to that the summation of Tot field and copy the result in data table .how to do that?
Get required data
var groupedData = from r in loans.AsEnumerable()
group r by new {
LoanCode = r.Field<int>("loan_code"),
EmpNum = r.Field<int>("emp_num")
} into g
select new {
g.Key.LoanCode,
g.Key.EmpNum,
Tot = g.Sum(r => r.Field<int>("Tot")) // assume integer
};
Then use custom CopyToDataTable method (which works for types that don't implement DataRow) to convert them to DataTable. Or you can build DataTable manually:
DataTable dt = new DataTable();
dt.Columns.Add("loan_code", typeof(int));
dt.Columns.Add("emp_num", typeof(int));
dt.Columns.Add("Tot", typeof(int));
foreach(var data in groupedData)
dt.Rows.Add(data.LoanCode, data.EmpNum, data.Tot);
I have DataTable object (OutputDT1), I want to use LINQ to group by column ConfirmedBy, then convert it back to a DataTable object which has only two columns ConfirmBy and Count.
var result = from row in OutputDT1.AsEnumerable()
group row by new
{
ConfirmedBy = row.Field<string>("ConfirmedBy")
}
into grp
select new
{
ConfirmedBy = grp.Key.ConfirmedBy,
Count = grp.Count(),
};
A simple way would be:
DataTable dt = new DataTable();
foreach(var item in result)
{
dt.Rows.Add(item.ConfirmedBy, item.count);
}
Using the solution from How to: Implement CopyToDataTable<T> Where the Generic Type T Is Not a DataRow
we can write:
var result = (from row in OutputDT1.AsEnumerable()
group row by row.Field<string>("ConfirmedBy") into grp
select new
{
ConfirmedBy = grp.Key,
Count = grp.Count(),
}).CopyToDataTable();
I have two DataTables : dt1 & dt2. dt1 contains one field, ID and dt2 contains two fields, ass_ID and Name.
I have to get the number of matched IDs from these two DataTables. How do I do this? Any easy way to compare them or anything to get the count of matched IDs (common IDs) in both of these tables?
var count = (from dr1 in dt.AsEnumerable()
from dr2 in dt2.AsEnumerable()
where dr1.Field<int>("ID") == dr2.Field<int>("ass_ID")
select dr1).Count();
Or
var count = (from dr1 in dt1.AsEnumerable()
join j in dt2.AsEnumerable() on dr1.Field<int>("ID") equals j.Field<int>("ass_ID")
select j).Count();
Try this:
string strExpression = string.Format("ID = '{0}'",dt2.Columns["ass_ID"]);
DafaultView dv = new DefaultView();
dv = dt1.DefaultView;
dv.RowFilter = strExpression;
//work with dv (DefaultView)