gpt4 book ai didi

c# - 在数据表上使用 LINQ 进行内部联接

转载 作者:太空宇宙 更新时间:2023-11-03 21:47:20 26 4
gpt4 key购买 nike

我有这 2 个数据表,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")

关于c# - 在数据表上使用 LINQ 进行内部联接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16257042/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com