使用 LInq to SQL 从 ASP.NET 会员资格获取所有用户电子邮件

发布于 2024-11-18 23:15:59 字数 1426 浏览 3 评论 0原文

public ActionResult DownloadMembers()
{
    MembershipUserCollection users = Membership.GetAllUsers();
    var grid = new WebControls.GridView();
    grid.DataSource = from members in users  // the users gives error
                      select new
                      {
                          Name = members.FirstName, // This doesn't works
                          Email = members.Email     //  This doesn't works
                      };
    grid.DataBind();
    Response.ClearContent();
    Response.AddHeader("content-disposition", "attachment; filename=" + "Members" + ".xls");
    Response.ContentType = "application/excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    grid.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();
    return RedirectToAction("Index");
}

您好,上面的查询将会员资格表中的所有详细信息下载为 MS Excel 的 CSV 格式,它给用户带来了一个错误:

找不到源类型“System.Web.Security.MembershipUserCollection”的查询模式的实现。未找到“选择”。考虑显式指定范围变量“成员”的类型。

任何人都可以指导我如何仅获取 csv 上的用户电子邮件地址和他/她的姓名,而不是获取所有详细信息。

编辑:这有效:)

grid.DataSource = from MembershipUser u in Membership.GetAllUsers()
                  select new
                  {
                      Firstname = u.UserName,
                      Email = u.Email                                   
                  };
public ActionResult DownloadMembers()
{
    MembershipUserCollection users = Membership.GetAllUsers();
    var grid = new WebControls.GridView();
    grid.DataSource = from members in users  // the users gives error
                      select new
                      {
                          Name = members.FirstName, // This doesn't works
                          Email = members.Email     //  This doesn't works
                      };
    grid.DataBind();
    Response.ClearContent();
    Response.AddHeader("content-disposition", "attachment; filename=" + "Members" + ".xls");
    Response.ContentType = "application/excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    grid.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();
    return RedirectToAction("Index");
}

Hi The above query downloads all the details from the membership table into CSV format for ms excel, it gives an error on users which is:

Could not find an implementation of the query pattern for source type 'System.Web.Security.MembershipUserCollection'. 'Select' not found. Consider explicitly specifying the type of the range variable 'members'.

Can any one guide me on how to get just the user email address and his/her name on the csv instead of getting all the details.

Edited: This works :)

grid.DataSource = from MembershipUser u in Membership.GetAllUsers()
                  select new
                  {
                      Firstname = u.UserName,
                      Email = u.Email                                   
                  };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

如此安好 2024-11-25 23:15:59
grid.DataSource = (from members in users  
                   select new
                   {
                       Name = members.FirstName,
                       Email = members.Email    
                   }).ToList();

我认为您需要调用 ToList() 因为 LINQ 会延迟加载,因此通过调用 ToList() 您实际上是显式强制加载数据。

grid.DataSource = (from members in users  
                   select new
                   {
                       Name = members.FirstName,
                       Email = members.Email    
                   }).ToList();

i think you need to call ToList()as LINQ does lazy loading, so by calling ToList() you actually are explicitly forcing to load data.

贪恋 2024-11-25 23:15:59
grid.DataSource = from members in users  
                  select new
                  {
                      Name = members.FirstName,
                      Email = members.Email    
                  };
grid.DataSource = from members in users  
                  select new
                  {
                      Name = members.FirstName,
                      Email = members.Email    
                  };
沐歌 2024-11-25 23:15:59
grid.DataSource = from MembershipUser u in Membership.GetAllUsers()
              select new
              {
                  Firstname = u.UserName,
                  Email = u.Email                                   
              };

上面的 linqQuery 工作正常。

grid.DataSource = from MembershipUser u in Membership.GetAllUsers()
              select new
              {
                  Firstname = u.UserName,
                  Email = u.Email                                   
              };

The above linqQuery works fine.

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