在 Lambda 表达式中选择 Func
专家您好 我想使用 Lambda 表达式并编写此代码:
Func<Regions, SelectAllRegion> Select = r => new {r.RegionID,r.RegionDescription};
clsr.SelectAll<SelectAllRegion>(Select);
但我不起作用。它引发此错误:
Cannot implicitly convert type 'AnonymousType#1' to 'SelectAllRegion'
我应该如何编写此代码?
Hi Experts
I want to use Lambda Expression and write this code:
Func<Regions, SelectAllRegion> Select = r => new {r.RegionID,r.RegionDescription};
clsr.SelectAll<SelectAllRegion>(Select);
but I does not works.It raise this Error:
Cannot implicitly convert type 'AnonymousType#1' to 'SelectAllRegion'
How should I write this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑您想要:
假设您有一个合适的 SelectAllRegion 类。您发布的 lambda 表达式将
Regions
转换为匿名类型 - 这始终是new { ... }
的结果。这只能(有用地)在类型推断的上下文中使用,而不是像这里那样的简单变量声明。I suspect you want:
That's assuming you've got a suitable
SelectAllRegion
class. The lambda expression you posted converts a aRegions
into an anonymous type - which is always the result ofnew { ... }
. That can only (usefully) be used in the context of type inference, rather than a simple variable declaration as you've got here.您的第一行返回匿名类型。您想要返回一个
SelectAllRegion
- 如下所示:Your first line returns an anonymous type. You want to return a
SelectAllRegion
- something like this: