C#Linq to Entities:join子句中某个表达式的类型不正确。(C# Linq to Entities: The type of one of the expressions in th

编程入门 行业动态 更新时间:2024-10-24 22:22:06
C#Linq to Entities:join子句中某个表达式的类型不正确。(C# Linq to Entities: The type of one of the expressions in the join clause is incorrect. Type inference failed)

我正在尝试使用C#linq连接两个表到实体。

一个表有客户端,另一个表有许可证。 这些表由Client_ID和ClientLicence_ClientID连接。 由于某种原因(它们不是我的表),Client_ID是一个i​​nt,而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 clientGroup

The key types are:

An anonymous type with a property called clientId An anonymous type with a property called ClientLicence_ClientID

Those 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.

更多推荐

本文发布于:2023-07-31 19:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1347343.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   不正确   句中   类型   join

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!