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);
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 defined a dataTable Like this
DataTable dtFinal = new DataTable();
dtFinal.Columns.Add("AVNR", typeof(int));
dtFinal.Columns.Add("Substation", typeof(string));
dtFinal.Columns.Add("ColumnTitle", typeof(string));
dtFinal.Columns.Add("S6_NAME", typeof(string));
dtFinal.Columns.Add("Voltage", typeof(string));
dtFinal.Columns.Add("Wert", typeof(decimal));
and I make a join between two tables to have a result set
var results = from table1 in dtTimeListTable.AsEnumerable()
join table2 in readyDataTable.AsEnumerable() on (decimal)table1["Avnr"] equals (int)table2["Avnr"]
select new
{
AVNR = (int)table2["AVNR"],
Substation = (string)table2["Substation"],
ColumnTitle = (string)table2["ColumnTitle"],
S6_NAME = (string)table2["S6_NAME"],
Voltage = (string)table2["Voltage"],
Wert = (decimal)table1["Wert"]
};
to fill datatable up I do the following:
dtFinal.Rows.Add(results.ToArray());
but I'll get a error liek this
input array is longer than the number of columns in this table
both datatable have 6 columns, what could be the problem?
DataRowCollection.Add is a method to add a single DataRow but you are trying to add all rows.
You need a loop:
foreach(var x in query)
dtFinal.Rows.Add(x.AVNR, x.Substation, x.ColumnTitle, x.S6_NAME, x.Voltage, x.Wert);
You could build the object[] for each DataRow also in this way:
var joinedRows = from table1 in dtTimeListTable.AsEnumerable()
join table2 in readyDataTable.AsEnumerable() on (decimal) table1["Avnr"] equals (int) table2["Avnr"]
select new { r1 = table1, r2 = table2 };
foreach (var x in joinedRows)
{
object[] fields =
{
x.r2.Field<int>("AVNR"), x.r2.Field<string>("Substation"), x.r2.Field<string>("ColumnTitle"),
x.r2.Field<int>("S6_NAME"), x.r2.Field<string>("Voltage"), x.r1.Field<decimal>("Wert"),
};
dtFinal.Rows.Add(fields);
}
I have created an application where the user can specify queries to databases, maybe even different databases. They would like to have functionality to join the two query results (stored in DataTables) together on user-specified criteria.
The user specifies the join criteria in an XML settings file like this:
<Join Name="join_example" TableAName="tbl_example1" TableBName="tbl_example2" Expression="a.ID == b.ID" />
So far i have converted the DataTables to List of dynamic so the column names are now properties, but I am getting the following error when trying to create the DynamicExpression using those properties:
{"No property or field 'ID' exists in type 'List`1'"}
Any ideas how i can create the dynamic expression? I am open to other ways to perform the join, but would like for the user to be able to use the syntax specified in the XML. Here is my code that is generating the error.
List<dynamic> TableA = ToDynamicList(DataTableA);
List<dynamic> TableB = ToDynamicList(DataTableB);
ParameterExpression paramA = System.Linq.Expressions.Expression.Parameter(TableA.GetType(), "a");
ParameterExpression paramB = System.Linq.Expressions.Expression.Parameter(TableB.GetType(), "b");
Expression Exp = System.Linq.Dynamic.DynamicExpression.Parse(new ParameterExpression[] { paramA, paramB }, TableA.GetType(), this.Expression, new List<dynamic>[] { TableA, TableB });
You don't need to cast the results from tables, you just need to merge them. Here is a linqpad example:
void Main()
{
var dt1 = new DataTable();
dt1.Columns.Add("col1", typeof(string));
dt1.Columns.Add("col2", typeof(int));
var dt2 = new DataTable();
dt2.Columns.Add("col1", typeof(string));
dt2.Columns.Add("col2", typeof(int));
var row = dt1.NewRow();
row["col1"] = "one";
row["col2"] = 1;
dt1.Rows.Add(row);
row = dt1.NewRow();
row["col1"] = "two";
row["col2"] = 2;
dt1.Rows.Add(row);
row = dt2.NewRow();
row["col1"] = "three";
row["col2"] = 3;
dt2.Rows.Add(row);
row = dt2.NewRow();
row["col1"] = "four";
row["col2"] = 4;
dt2.Rows.Add(row);
var dtMerged = dt1.AsEnumerable().CopyToDataTable(); // Note: CopyToDataTable requirs that there are rows. must trap for empty table
dtMerged.Merge(dt2.AsEnumerable().CopyToDataTable(), true, MissingSchemaAction.Add);
dtMerged.Dump();
}
I have this sql query and I want to convert it to linq.
SELECT [Scheme_Code], [FundFamily], [Scheme_Name], MAX([Date]) as LastDate
FROM [MFD].[dbo].[MFDatas]
GROUP BY [Scheme_Code], [Scheme_Name], [FundFamily]
ORDER BY [Scheme_Code]
I want to check last date and if it's latest then I have to mark it as 'live'?
Since you want to copy results to DataTable, this query does what specified in your Sql query and copies results to DataTable.
DataTable dt = new DataTable();
dt.Columns.Add("Scheme_Code", typeof(string));
dt.Columns.Add("Scheme_Name", typeof(string));
dt.Columns.Add("FundFamily", typeof(string));
dt.Columns.Add("LastDate", typeof(DateTime));
var table = MFDatas.GroupBy(g=> new { Scheme_Code, Scheme_Name, FundFamily})
.Select(s=>
{
var row = dt.NewRow();
row["Scheme_Code"] = s.Key.Scheme_Code,
row["Scheme_Name"] = s.Key.Scheme_Name,
row["FundFamily"] = s.Key.FundFamily,
row["LastDate"] = s.Max(m=>m.Date)
})
.OrderBy(o=>o.Field<string>("Scheme_Code"))
.Distinct()
.CopyToDataTable();
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();