将 LinQ 结果转换为强类型集合时出现问题
我想这很微不足道,但对我来说不是。
我有一个集合MembershipUserCollection
。我想对其执行一些 Linq 查询,因此我使用了 OfType
方法,然后对其应用了 Where()
。但是,我不知道如何将结果转换回 MembershipUserCollection
?
这是我的代码:
MembershipUserCollection Users;
Users = GetAllUsers(pageIndex, pageSize, out totalRecords).OfType<MembershipUser>().Where(user => user.Email == emailToMatch); //how to cast it back to MembershipUserCollection ?
GetAllUsers(pageIndex, pageSize, out totalRecords)
返回MembershipUserCollection
感谢您的宝贵时间。
I guess this is trivial but not for me.
I have a collection MembershipUserCollection
. I wanted to perform some Linq query over it so I've used OfType<MembershipUser>()
method and then applied Where()
on it. However, I do not know how to cast back my results to MembershipUserCollection
?
This is my code:
MembershipUserCollection Users;
Users = GetAllUsers(pageIndex, pageSize, out totalRecords).OfType<MembershipUser>().Where(user => user.Email == emailToMatch); //how to cast it back to MembershipUserCollection ?
GetAllUsers(pageIndex, pageSize, out totalRecords)
is returning MembershipUserCollection
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您不能在查询本身中执行此操作,但您应该能够执行此操作:(
当然,如果您愿意,您可以将该逻辑包装到扩展方法中,然后在询问。)
I don't think you can do it in the query itself, but you should be able to do this:
(And, of course, you could wrap that logic up into an extension method if you wanted and then call the extension method in the query.)
可能的解决方案之一:
另一种带有 foreach 语句的解决方案:
One of the possible solutions:
Another one with foreach statement:
所以这很简单,我只需这样投射:
谢谢大家的回答。
So it was trivial, I just had to cast it like this:
Thanks guys for your asnwers.