LAMDA链接类型参数不能从用法中推断出
我们正在使用实体,我正在尝试在2列上加入2个表。 ID和服务器ID。我的原始查询没有说明服务器(这是我要修复的用途)
var client = _Context.Address
.Join(_Context.Clients.Where(c => c.Key == Key),
a => a.AddressId,
c => c.AddressId,
(a, c) => a ).FirstOrDefault();
,这起作用了。但是,当我添加服务器时,
var client = _Context.Address
.Join(_Context.Clients.Where(c => c.Key == Key),
a => new { a.AddressId, a.ServerId},
c => new { c.AddressId, c.ServerId},
(a, c) => a ).FirstOrDefault();
JOIN现在将标题中的错误抛出。在我的研究中,我看到名称和类型必须匹配。这些名称确实匹配,而我试图将它们分配给双重确定,但这并不能解决问题。查看实体中的定义,列是地址表中的int,但在客户端表中是无效的。
如何将INT列连接到无效的INT?
编辑: 客户端表中
public int? AddressId { get; set; }
public int? ServerId { get; set; }
从地址表中的
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int AddressId { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ServerId { get; set; }
we are using entity and I am trying to join 2 tables on 2 columns. an id and server id. My original query didn't account for server (which is what i'm trying to fix)
var client = _Context.Address
.Join(_Context.Clients.Where(c => c.Key == Key),
a => a.AddressId,
c => c.AddressId,
(a, c) => a ).FirstOrDefault();
and this works. But when I add in the server
var client = _Context.Address
.Join(_Context.Clients.Where(c => c.Key == Key),
a => new { a.AddressId, a.ServerId},
c => new { c.AddressId, c.ServerId},
(a, c) => a ).FirstOrDefault();
the join now throws the error in the title. In my research I see that the names and types have to match. The names do match and ive tried assigning them to be doubly sure but that doesn't resolve the issue. Looking at the definition in entity the columns are int in the address table but are nullable int in the client table.
How can I join an int column to an nullable int?
edit:
from the client table
public int? AddressId { get; set; }
public int? ServerId { get; set; }
from the address table
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int AddressId { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ServerId { get; set; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使它们具有相同的类型,否则编译器将无法决定应该是什么,因为
anontype< int,int>
和anontype< int?,int?> ,例如,施放了不可用的ints be
(int?)
,因此它们匹配您以前逃脱了它,因为从中有隐含的转换
int
toint?
对于单个密钥参数,但是如果您在JOIN密钥中使用了单个Prop的匿名类型,则它会同样错误:You need to make them the same type, otherwise the compiler won't be able to decide what TKey is supposed to be because there isn't an implicit conversion between
AnonType<int, int>
andAnonType<int?, int?>
e.g. cast the non-nullable ints to be(int?)
so they matchYou got away with it before because there was an implicit conversion from
int
toint?
for the single key argument, but it would have similarly errored if you'd used anonymous types with a single prop in your join keys: