如何通过LINQ获取单个对象?错误:查询的结果不能多​​次枚举

时间:2012-03-17 12:47:46

标签: c# linq entity-framework

我的代码是这样的:

    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();
    }

我在最后一行收到错误:

异常详细信息:System.InvalidOperationException:无法多次枚举查询结果。

然后我修改了这个函数(感谢@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();
}

仍然是同样的错误!!!!

会发生什么!?!?

“aspnet_Users”属性:

    /// <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);
            }
        }
    }

5 个答案:

答案 0 :(得分:3)

您可以使用SingleSingleOrDefault()

// parse the string `ddlRoles.SelectedValue` into the appropriate type as `roleID`
var roleID = ParseRoleID(ddlRoles.SelectedValue);
var aRole = (from rol in aspnet_Roles.Include("aspnet_Users")
            where rol.RoleId == roleID 
            select rol).Single();

但是,如果返回了多个实体,则会抛出异常。如果您只想要与案例匹配的第一个实体,可以使用First()FirstOrDefault()

var roleID = ParseRoleID(ddlRoles.SelectedValue);
var aRole = (from rol in aspnet_Roles.Include("aspnet_Users")
            where rol.RoleId == roleID 
            select rol).First();

这些方法的OrDefault版本确保在没有匹配项时不会出现异常。它将返回null。

此外:
而不是在ToString()上使用RoleIdddlRoles.SelectedValue解析为适当的类型,然后在LINQ查询中使用它,因为某些方法(ToString()是其中之一)不是支持在针对数据库执行的LINQ查询中。

答案 1 :(得分:1)

你可以这样做:

var aRole = _allRoles.ToList().FirstOrDefault(rol => rol.RoleId.ToString() == ddlRoles.SelectedValue);

答案 2 :(得分:1)

var aRole = _allRoles.Single(x=>x.RoleId.ToString() == ddlRoles.SelectedValue);

如果我错了,请纠正我,但这也是一个可能的选择

答案 3 :(得分:0)

这样的事情会:

private void BindGvUsersInRole () 
    { 
        var aUsers = 
            (from aspnet_Roles rol in _allRoles
from u in rol.aspnet_Users
            where rol.RoleId.ToString() == ddlRoles.SelectedValue 
            select u).ToList(); 

        this.gvUsersInRole.DataSource = aUsers; 
        this.gvUsersInRole.DataBind(); 
    } 

答案 4 :(得分:0)

private void BindGvUsersInRole ()
{
    var roleID = new Guid(ddlRoles.SelectedValue);

    var users = (from rol in _allRoles
                 where rol.RoleId == roleID
                 select rol.aspnet_Users).ToList();

    this.gvUsersInRole.DataSource = users;
    this.gvUsersInRole.DataBind();
}