TableCellCollection 上的 Linq - C#
void foo (TableCellCollection bar)
{
bar.Cast<TableCellCollection>().Where(a...
}
在上面的代码中,lambda 'a
' 仍然是 TableCellCollection
而不是 TableCell
,任何人都可以指出我做错了什么? 谢谢!
void foo (TableCellCollection bar)
{
bar.Cast<TableCellCollection>().Where(a...
}
On the code above, the lambda 'a
' is still a TableCellCollection
rather than a TableCell
, can anyone point out what I'm doing wrong?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,您已经通过
Cast
调用告诉它应该是TableCellCollection
。如果您想将每个元素转换为TableCell
,那么您应该提供类型参数:Yes, you've told it that it should be
TableCellCollection
with yourCast
call. If you want to cast each element toTableCell
, that's the type argument you should give:除了乔恩的回答之外,您的代码将在运行时导致强制转换异常。 Cast 方法适用于
IEnumerable
集合,而不是IEnumerable
。它将表现得好像您正在执行以下操作:Further to Jon's answer your code would cause a cast exception at runtime. The Cast method works on
IEnumerable
collections notIEnumerable<t>
. It would act as if you were doing the below:我发现这很有用
,我使用 Linq 查询集合以查找字符串,然后更改它。如果您正在填充网格视图并想要更改标题,那么它很有用。
I found this useful
I use Linq to query the collection looking for a string and then change it. Its usefull if you are filling a gridview and want to change the headers.