My code like this:
private void BindGvUsersInRole ()
{
var aRole =
(from aspnet_Roles rol
in _allRoles
where rol.RoleId.ToString() == ddlRoles.SelectedValue
select rol).SingleOrDefault();
this.gvUsersInRole.DataSource = aRole.aspnet_Users;
this.gvUsersInRole.DataBind();
}
I got an error on the last line:
Exception Details: System.InvalidOperationException: The result of a query cannot be enumerated more than once.
then I amend the function like this(thanks @VitaliyKalinin):
private void BindGvUsersInRole ()
{
Guid roleID=new Guid(ddlRoles.SelectedValue);
var users =
(
from aspnet_Roles rol in _allRoles
from u in rol.aspnet_Users
where rol.RoleId == roleID
select u
).ToList();
this.gvUsersInRole.DataSource = users;
this.gvUsersInRole.DataBind();
}
STILL SAME ERROR!!!!
what happens!?!?
the “aspnet_Users” property :
/// <summary>
/// 没有元数据文档可用。
/// </summary>
[XmlIgnoreAttribute()]
[SoapIgnoreAttribute()]
[DataMemberAttribute()]
[EdmRelationshipNavigationPropertyAttribute("realtydbModel", "aspnet_UsersInRoles", "aspnet_Users")]
public EntityCollection<aspnet_Users> aspnet_Users
{
get
{
return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<aspnet_Users>("realtydbModel.aspnet_UsersInRoles", "aspnet_Users");
}
set
{
if ((value != null))
{
((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<aspnet_Users>("realtydbModel.aspnet_UsersInRoles", "aspnet_Users", value);
}
}
}
You can use
SingleorSingleOrDefault()todo this:However this will throw an exception if there is more than one entity returned. If you just want the first entity that matches the case you can use
First()orFirstOrDefault():The
OrDefaultversions of these methods make sure you don’t get an exception if there is no match. It will return null instead.Also:
Rather than using
ToString()on theRoleIdparse theddlRoles.SelectedValueinto the appropriate type before you use it in the LINQ query since some methods (ToString()is one of them) are not supported within a LINQ query that executes against a DB.