将 LinQ 结果转换为强类型集合时出现问题

发布于 2024-09-28 11:07:35 字数 636 浏览 0 评论 0原文

我想这很微不足道,但对我来说不是。

我有一个集合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 技术交流群。

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

发布评论

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

评论(3

顾忌 2024-10-05 11:07:35

我认为您不能在查询本身中执行此操作,但您应该能够执行此操作:(

var query = GetAllUsers(pageIndex, pageSize, out totalRecords)
                .OfType<MembershipUser>()
                .Where(user => user.Email == emailToMatch);

var users = new MembershipUserCollection();
foreach (var user in query)
{
    users.Add(user);
}

当然,如果您愿意,您可以将该逻辑包装到扩展方法中,然后在询问。)

I don't think you can do it in the query itself, but you should be able to do this:

var query = GetAllUsers(pageIndex, pageSize, out totalRecords)
                .OfType<MembershipUser>()
                .Where(user => user.Email == emailToMatch);

var users = new MembershipUserCollection();
foreach (var user in query)
{
    users.Add(user);
}

(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.)

本宫微胖 2024-10-05 11:07:35

可能的解决方案之一:

   var users = GetAllUsers(pageIndex, pageSize, out totalRecords)
      .OfType<MembershipUser>()
      .Where(user => user.Email == emailToMatch);

   MembershipUserCollection membershipUsers = new MembershipUserCollection();
   users
      .ToList()
      .ForEach(membershipUsers.Add);

另一种带有 foreach 语句的解决方案:

foreach (var user in users)
{
    membershipUsers.Add(user);
}

One of the possible solutions:

   var users = GetAllUsers(pageIndex, pageSize, out totalRecords)
      .OfType<MembershipUser>()
      .Where(user => user.Email == emailToMatch);

   MembershipUserCollection membershipUsers = new MembershipUserCollection();
   users
      .ToList()
      .ForEach(membershipUsers.Add);

Another one with foreach statement:

foreach (var user in users)
{
    membershipUsers.Add(user);
}
看透却不说透 2024-10-05 11:07:35

所以这很简单,我只需这样投射:

MembershipUserCollection Users;
Users = (MembershipUserCollection)GetAllUsers(pageIndex, pageSize, out totalRecords).OfType<MembershipUser>().Where(user => user.Email == emailToMatch).Cast<MembershipUser>();

谢谢大家的回答。

So it was trivial, I just had to cast it like this:

MembershipUserCollection Users;
Users = (MembershipUserCollection)GetAllUsers(pageIndex, pageSize, out totalRecords).OfType<MembershipUser>().Where(user => user.Email == emailToMatch).Cast<MembershipUser>();

Thanks guys for your asnwers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文