使用 LInq to SQL 从 ASP.NET 会员资格获取所有用户电子邮件
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您需要调用
ToList()
因为 LINQ 会延迟加载,因此通过调用ToList()
您实际上是显式强制加载数据。i think you need to call
ToList()
as LINQ does lazy loading, so by callingToList()
you actually are explicitly forcing to load data.上面的 linqQuery 工作正常。
The above linqQuery works fine.