2013-04-27 32 views
1

我有這2個DataTables,customerTableDTcustomerAliasesTableDT。他們都是從數據庫中填充的是這樣的:在DataTable上使用LINQ進行內部聯接

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

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

現在,我試圖做一個內部連接在兩個DataTable這樣的:

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") 

但它給我這個錯誤:

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

如果我不得不在SQL寫什麼,我試圖做的,它很簡單:

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

任何幫助?

回答

4

AsEnumerable後錯過了括號,所以它視爲方法組,不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") 
+0

哈哈...好找!謝謝 ! – Ahmad 2013-04-27 21:49:11