Ahmad
Ahmad

Reputation: 13436

Inner join using LINQ on DataTables

I have these 2 DataTables, customerTableDT and customerAliasesTableDT. They are both populated from a database like this:

customerTableDT = UtilityDataAndFunctions.PerformDBReadTransactionDataTableFormat(String.Format("SELECT * FROM {0}", TableNames.customers));

customerAliasesTableDT = UtilityDataAndFunctions.PerformDBReadTransactionDataTableFormat(String.Format("SELECT * FROM {0}", TableNames.customerAliases));

Now I'm trying to do an inner join on the two datatables like this:

var customerNames = from customers in customerTableDT.AsEnumerable()
                    join aliases in customerAliasesTableDT.AsEnumerable on customers.Field<int>("CustomerID") equals aliases.Field<int>("CustomerID")
                    where aliases.Field<string>("Alias").Contains(iString) select customers.Field<string>("Name")

But it gives me this error:

The type of one of the expressions in the join clause is incorrect.  Type inference failed in the call to 'Join'.

If I had to write in SQL about what I'm trying to do, its very simple:

SELECT * FROM CUSTOMERS C
INNER JOIN CustomerAliases ALIASES ON ALIASES.CustomerID = C.CustomerID
WHERE CA.Alias LIKE %STRING_HERE%

Any help ?

Upvotes: 1

Views: 12331

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125650

You missed brackets after AsEnumerable, so it's treated as Method Group, not IEnumerable<DataRow>:

var customerNames = from customers in customerTableDT.AsEnumerable()
                    join aliases in customerAliasesTableDT.AsEnumerable() on customers.Field<int>("CustomerID") equals aliases.Field<int>("CustomerID")
                    where aliases.Field<string>("Alias").Contains(iString) select customers.Field<string>("Name")

Upvotes: 5

Related Questions