I want to select data of a particular column of all records using Linq command.Here is my code
[HttpGet]
public ActionResult DeleteRole(RoleManager model)
{
string [] getRole = (from r in Session.Query<RoleManager>()
select r.roleName).ToArray();
foreach( var myrole in getRole)
{
model.roleName = myrole.roleName;
}
return View(model);
}
but it is not working.Can you please guide me.
You're trying to select roleName
twice - you've already done the right thing by selecting it the first time. You're also overwriting the roleName
field/property in the model multiple times, whereas presumably you want to retain all the values. It's not clear what model
is meant to be doing at all, to be honest - but you should be able to create a view of just the roles:
[HttpGet]
public ActionResult DeleteRole(RoleManager model)
{
string[] roles = (from r in Session.Query<RoleManager>()
select r.roleName).ToArray();
return View(roles);
}
Or using the Select
extension method directly, which is simpler in this case IMO:
[HttpGet]
public ActionResult DeleteRole(RoleManager model)
{
string[] roles = Session.Query<RoleManager>()
.Select(r => r.roleName)
.ToArray();
return View(roles);
}
... or if you do need to modify the model, you'll need the model itself to support multiple roles.
Note that this isn't actually doing any deleting, which makes it a fairly odd action name in my opinion...
See more on this question at Stackoverflow