LAMDA链接类型参数不能从用法中推断出

发布于 2025-01-22 08:59:42 字数 1182 浏览 3 评论 0原文

我们正在使用实体,我正在尝试在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤芳又自赏 2025-01-29 08:59:42

如何将int列连接到可无效的int?

您需要使它们具有相同的类型,否则编译器将无法决定应该是什么,因为anontype< int,int>anontype< int?,int?> ,例如,施放了不可用的ints be (int?) ,因此它们匹配

Join(_Context.Clients.Where(c => c.Key == Key),
  a => new { AddressId = (int?)a.AddressId, ServerId = (int?)a.ServerId},
  c => new { c.AddressId, c.ServerId},

您以前逃脱了它,因为从中有隐含的转换int to int?对于单个密钥参数,但是如果您在JOIN密钥中使用了单个Prop的匿名类型,则它会同样错误:

//this would fail too
Join(_Context.Clients.Where(c => c.Key == Key),
  a => new { AddressId }, 
  c => new { c.AddressId },

How can I join an int column to an nullable int?

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> and AnonType<int?, int?> e.g. cast the non-nullable ints to be (int?) so they match

Join(_Context.Clients.Where(c => c.Key == Key),
  a => new { AddressId = (int?)a.AddressId, ServerId = (int?)a.ServerId},
  c => new { c.AddressId, c.ServerId},

You got away with it before because there was an implicit conversion from int to int? 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:

//this would fail too
Join(_Context.Clients.Where(c => c.Key == Key),
  a => new { AddressId }, 
  c => new { c.AddressId },
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文