繁体   English   中英

在DataTables上使用LINQ进行内部联接

[英]Inner join using LINQ on DataTables

我有这两个DataTables, customerTableDTcustomerAliasesTableDT 它们都是从这样的数据库填充的:

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

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

现在我正在尝试对这两个数据表进行内连接:

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%

有帮助吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM