我正在尝试使用C#linq连接两个表到实体。
一个表有客户端,另一个表有许可证。 这些表由Client_ID和ClientLicence_ClientID连接。 由于某种原因(它们不是我的表),Client_ID是一个int,而ClientLicence_ClientID是一个字符串。
我的代码是:
var licences = (from client in entities.Clients join licence in entities.ClientLicences on new { clientId = SqlFunctions.StringConvert((double)client.Client_ID) } equals new { licence.ClientLicence_ClientID } into clientGroup where (filter.clientId == 0 || client.Client_ID == filter.clientId) select licence);当我尝试编译它时,我得到一个错误:join子句中的一个表达式的类型是不正确的。 在对“GroupJoin”的调用中类型推断失败。
如果我注释掉where语句那么它可以工作(尽管没有过滤结果)。
filter变量是传递给函数的类。 它只有一个属性:int clientId。
I am trying to join two tables together using C# linq to entities.
One table has clients, the other table has licences. The tables are joined by Client_ID and ClientLicence_ClientID. For some reason (they are not my tables) the Client_ID is an int, and the ClientLicence_ClientID is a string.
The code I have is:
var licences = (from client in entities.Clients join licence in entities.ClientLicences on new { clientId = SqlFunctions.StringConvert((double)client.Client_ID) } equals new { licence.ClientLicence_ClientID } into clientGroup where (filter.clientId == 0 || client.Client_ID == filter.clientId) select licence);When I try to compile this I get an error: The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'GroupJoin'.
If I comment out the where statement then it works (albeit without filtering results).
The filter variable is a class that is passed in to the function. It only has one propery: int clientId.
最满意答案
看看你的加入:
join licence in entities.ClientLicences on new { clientId = SqlFunctions.StringConvert((double)client.Client_ID) } equals new { licence.ClientLicence_ClientID } into clientGroup关键类型是:
具有名为clientId的属性的匿名类型 具有名为ClientLicence_ClientID的属性的匿名类型这两个匿名类型无法进行相等性比较。 实际上,我怀疑你根本不需要匿名类型。 我希望这可行:
join licence in entities.ClientLicences on SqlFunctions.StringConvert((double)client.Client_ID) equals licence.ClientLicence_ClientID into clientGroup(显然你不需要所有的新行 - 我只是为了清晰起见而试图将各个部分分开。)
你声称没有where子句它可以工作 - 但这将是非常令人惊讶的,因为它是一个问题的连接。
Look at your join:
join licence in entities.ClientLicences on new { clientId = SqlFunctions.StringConvert((double)client.Client_ID) } equals new { licence.ClientLicence_ClientID } into clientGroupThe key types are:
An anonymous type with a property called clientId An anonymous type with a property called ClientLicence_ClientIDThose two anonymous types can't be compared for equality. I suspect you don't need anonymous types at all here, actually. I'd expect this to work:
join licence in entities.ClientLicences on SqlFunctions.StringConvert((double)client.Client_ID) equals licence.ClientLicence_ClientID into clientGroup(Obviously you don't need all the new lines - I was just trying to separate out the various bits for clarity.)
You claim that without the where clause it works - but that would be very surprising, given that it's the join that's a problem.
更多推荐
发布评论