Using LInq to SQL to get all the user email from ASP.NET Membership
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.Conte开发者_开发问答ntType = "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
};
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.
grid.DataSource = from members in users
select new
{
Name = members.FirstName,
Email = members.Email
};
grid.DataSource = from MembershipUser u in Membership.GetAllUsers()
select new
{
Firstname = u.UserName,
Email = u.Email
};
The above linqQuery works fine.
精彩评论