I would like to do something create dynamic column in Linq for my joined table.
This is my code after asking around. But it give me another error like this.
Code:
var SelectedDT1AndDt2= from dr1 in dt1.AsEnumerable()
join dr2 in dt2.AsEnumerable()
on dr1.Field<Int64>("id1") equals
dr2.Field<Int64>("id2") into joinDt1AndDt2
from leftjoin in joinDt1AndDt2.DefaultIfEmpty()
select dtJoinedTable.LoadDataRow(
(from dc1 in dt1.Columns.Cast<DataColumn>()
select dc1.ColumnName.ToString()).ToArray()
.Union(from dc2 in dt2.Columns.Cast<DataColumn>()
select dc2.ColumnName).ToArray()
, false);
SelectedDT1AndDt2.CopyToDataTable();
Error:
Input string was not in a correct format.Couldn't store in id Column. Expected type is Int64.
This is because the column import is not type Int'64', I check again and confirm that the original col type is Int'32'.
Related
I'm trying to filter my SqlDataSource on a column of a JOIN but I get this error:
The multi-part identifier "B.Name" could not be bound
SqlDataSource.SelectCommand:
SELECT MD.MediaDocumentID, MD.Name, B.BrandID, B.Name as BrandName
FROM MediaDocument MD
JOIN Brand B ON B.BrandID = MD.BrandID
WHERE MD.Active = 1
In code behind I'm using the method FilterExpression like this:
sqldatasource.FilterExpression = "(B.Name LIKE '%phone%')";
Thank you!
I have a general understanding issue of Linq as it seems. I tried already to find the answer in other threads but was not successful.
Why does this query not work ? I have two tables: one with customer data, the other with order data and try now to join them. But it fails and I don't understand yet why ? In my opinion it is done as in any other tutorial.
IEnumerable<DataRow> query = from cust_ in Cust.AsEnumerable()
join order_ in Orders.AsEnumerable() on cust_.Field<int>("ID") equals order_.Field<int>("Customer ID")
select new { customer = cust_.Field<string>("LastName") }
This is the error meassge it throws:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<<anonymous type: System.Data.DataRow cust_, System.Data.DataRow order_>>' to 'System.Collections.Generic.IEnumerable<System.Data.DataRow>'. An explicit conversion exists (are you missing a cast ?
Your select is not returning a DataRow, it's returning an anonymous type. Either drop the 'new' and return a string,
IEnumerable<string> query = from cust_ in Cust.AsEnumerable()
join order_ in Orders.AsEnumerable() on cust_.Field<int>("ID")
equals order_.Field<int>("Customer ID")
select cust_.Field<string>("LastName")
or maybe better don't specify the type, instead try:
var query = from cust_ in Cust.AsEnumerable()
join order_ in Orders.AsEnumerable() on cust_.Field<int>("ID")
equals order_.Field<int>("Customer ID")
select new { customer = cust_.Field<string>("LastName") }
I using the linq to left join another data table, this make me need to using Linq to create columns for new leftjoin data table.
Is it anyway to loop through the columns to create it automatically in linq?
var Dt1JoinDt2 = from dr1 in dt1.AsEnumerable()
join dr2 in dt2.AsEnumerable()
on dr1.Field<Int64>(id1) equals dr2.Field<Int64>(id2) into joinDt1AndDt2
from leftjoin in joinDt1AndDt2.DefaultIfEmpty()
select dtJoinedTable.LoadDataRow(new object[]
{
dr1.Field<String>("id"),
dr1.Field<Int64?>("col_sample"),
dr1.Field<String>("col2"),
dr1.Field<String>("colName"),
dr1.Field<String>("columns"),
dr1.Field<String>("col_no"),
......
This is the portion of code I want to use loop to create the columns
dr1.Field<String>("id"),
dr1.Field<Int64?>("col_sample"),
dr1.Field<String>("col2"),
dr1.Field<String>("colName"),
dr1.Field<String>("columns"),
dr1.Field<String>("col_no"),
......
Is it anyway to do this by Linq or C# or others coding method???
I think you still need to cast to DataColumn
using System.Data
var Dt1JoinDt2 = from dr1 in dt1.AsEnumerable()
join dr2 in dt2.AsEnumerable()
on dr1.Field<Int64>(id1) equals dr2.Field<Int64>(id2) into joinDt1AndDt2
from leftjoin in joinDt1AndDt2.DefaultIfEmpty()
select dtJoinedTable.LoadDataRow(
(from dc1 in dt1.Columns.Cast<DataColumn>() select dc1.ColumnName).ToArray() // cast to DataColumn, use joinDt1AndDt2
.Union((from dc2 in dt2.Columns.Cast<DataColumn> select dc2.ColumnName).ToArray())
)
I don't have a computer in hand but you can try something like:
dt1.Columns.AsEnumerable().Select(column => dr1[column.Name]).ToArray()
I am quite new to this, I am running two SQL queries and I am creating two separate data tables, DataTable1 and DataTable2.
I am applying some linq criteria to DataTable1 and creating another data table from that, which is DataTable3.
var Query3 = from table1 in DataTable1.AsEnumerable()
where table1.Field<DateTime>("DateTime") <= Yday
where table1.Field<string>("StockCode").Contains("-CA") && !(table1.Field<string>("StockCode").Contains("-CAB")) ||
table1.Field<string>("StockCode").Contains("-CM") ||
table1.Field<string>("StockCode").Contains("-LP")
select table1;
DataTable DataTable3 = Query3.CopyToDataTable()
Now I would write another query to do the following.
Both data tables have a column JobNumber. I would like to query DataTable3 in DataTable 2 to count the rows that have similar JobNumber entries. Below is what I am doing but I am not getting the correct count.
int count = (from table3 in DataTable3.AsEnumerable()
join table2 in DataTable2.AsEnumerable() on table2.Field<string>("JobNumber") equals table3.Field<string>("JobNumber")
where table2.Field<string>("JobNumber") == table3.Field<string>("JobNumber")
select table2).Count();
You are creating a cartesian join and counting its result, was that what you indented ? Also in your linq your Join expression and where expression is same (where is redundant). It is not clear what you really want to count. Probably you instead wanted to count those in DataTable2 where JobNumbers exists in DataTable3?:
var jobNumbers = (from r in DataTable3.AsEnumerable()
select r.Field<string>("JobNumber")).ToList();
var count = (from r in DataTable2.AsEnumerable()
where jobNumbers.Contains( r.Field<string>("JobNumber") )
select r).Count();
As a side note, it would be much easier if you used Linq To SQL instead (rather than Linq To DataSet).
I am trying to convert sql query for select to linq query using EF in MVC but really got stuck with an error.
In SQL I'm able to get 6 records for my query,similarly when I try to convert this to linq it shows some error.
Following is my query in SQL:
SELECT
PurchaseOrderMaster.*, PurchaseOrderDetails.*, Vendor.*,
BusinessUnit.*, InvoiceMaster.*, TenantEmployee.*
FROM
PurchaseOrderMaster
INNER JOIN
PurchaseOrderDetails ON PurchaseOrderMaster.TenantID = PurchaseOrderDetails.TenantID
AND PurchaseOrderMaster.PurchaseOrderNumber = PurchaseOrderDetails.PurchaseOrderNumber
AND PurchaseOrderMaster.PurchaseOrderDate = PurchaseOrderDetails.PurchaseOrderDate
INNER JOIN
InvoiceMaster ON PurchaseOrderMaster.TenantID = InvoiceMaster.TenantID
AND PurchaseOrderMaster.PurchaseOrderNumber = InvoiceMaster.PurchaseOrderNumber
AND PurchaseOrderMaster.PurchaseOrderDate = InvoiceMaster.PurchaseOrderDate
INNER JOIN
BusinessUnit ON PurchaseOrderMaster.TenantID = BusinessUnit.TenantID
AND PurchaseOrderMaster.BusinessUnitID = BusinessUnit.BusinessUnitID
INNER JOIN
TenantEmployee ON PurchaseOrderMaster.TenantID = TenantEmployee.TenantID
INNER JOIN
Vendor ON PurchaseOrderMaster.TenantID = Vendor.TenantID
AND PurchaseOrderMaster.VendorID = Vendor.VendorID
For this query I am able to get 6 records .
And my linq query is:
return (from pom in db.PurchaseOrderMaster
join pod in db.PurchaseOrderDetails on pom.TenantID equals pod.TenantID
where pom.PurchaseOrderNumber == pod.PurchaseOrderNumber && pom.PurchaseOrderDate == pod.PurchaseOrderDate
join inv in db.InvoiceMaster on pom.TenantID equals inv.TenantID
where pom.PurchaseOrderNumber == inv.PurchaseOrderNumber && pom.PurchaseOrderDate == inv.PurchaseOrderDate
join bu in db.BusinessUnit on pom.BusinessUnitID equals bu.BusinessUnitID
join te in db.TenantEmployee on pom.TenantID equals te.TenantID
join v in db.Vendor on pom.TenantID equals v.TenantID
where pom.VendorID == v.VendorID
orderby pom.PurchaseOrderNumber ascending, pom.PurchaseOrderDate descending
select new { pom, pod, inv, bu, te, v }).ToList();
At the time of debugging,following is the error that I'm getting:
{"Invalid column name 'invoiceMasterModel_TenantID'.\r\nInvalid column name 'invoiceMasterModel_PurchaseOrderNumber'.\r\nInvalid column name 'invoiceMasterModel_PurchaseOrderDate'.\r\nInvalid column name 'invoiceMasterModel_InvoiceNumber'.\r\nInvalid column name 'invoiceMasterModel_InvoiceDate'.\r\nInvalid column name 'tenantEmployeeModel_TenantID'.\r\nInvalid column name 'tenantEmployeeModel_EmployeeID'."}
Inside Invoice Table it is not able to find some of the columns and hence throwing the error according to me..
I tried with many possible ways but was unable to solve this.
Any ideas..?
Problem was with my Entity.
What I did is,I added my entity again and according to that I recreated models for the associated tables removing the earlier ones.
It solved my problem finally .
I found this link Entity Framework 5 Invalid Column Name error related to somewhat similar problem.
Here also similar kind of error happened after the date time field. Check if your datetime field PurchaseOrderDate is nullable.
Many tools exist that can convert your sql queries to linq, in case you don't wanna write it urself. Try the following sites, works well in my case:
http://www.sqltolinq.com/
http://www.linqpad.net/