将元素添加到键值对列表
Adding elements to a keyvaluepair List
我有以下代码,想将它添加到键值对 List/Array/Dictionary 中,以效果最好的为准。
一点背景:
目前,这个片段直接与数据库交互,它在一个循环中,在每次迭代时将数据推送到数据库。我的总体意图是将此数据推送到 list/dictionary 等,以便我可以一次将所有数据推送到数据库。这是:
_dataContext.GetRepository<UserRole, Guid>().Insert(new[]
{
new UserRole
{
Id = Guid.NewGuid(),
UserId = newGroup.CreatedById,
RoleId = SecurityConstants.RoleId.GROUP_ADMINISTRATOR,
GroupId = newGroup.Id,
CreatedById = newGroup.CreatedById
},
new UserRole
{
Id = Guid.NewGuid(),
RoleId = SecurityConstants.RoleId.CONTACT_PERSON,
UserId = newGroup.CreatedById,
GroupId = newGroup.Id,
CreatedById = newGroup.CreatedById
}
});
上面的 GetRepository 然后访问数据库,如上所述。我想以任何可行的方式单独存储数据,以便稍后将其推送到数据库
public interface IMydDataContext : IDisposable
{
IRepository<TEntity, TKrey> GetRepository<TEntity, TKrey>() where TEntity : class, IDomainObject<TKrey>;
IDbSet<TEntity> Set<TEntity, TKey>() where TEntity : class, IDomainObject<TKey>;
int SaveChanges();
Task<int> SaveChangesAsync();
}
我在这里声明字典:
Dictionary<UserRole, Guid> _usersRepositoryDictionary = new Dictionary<UserRole, Guid>();
并尝试以这种方式将数据放入其中:
_usersRepositoryDictionary.Add(new[] { new UserRole
{
Id = Guid.NewGuid(),
UserId = newGroup.CreatedById,
RoleId = SecurityConstants.RoleId.GROUP_ADMINISTRATOR,
GroupId = newGroup.Id,
CreatedById = newGroup.CreatedById
},
new UserRole
{
Id = Guid.NewGuid(),
RoleId = SecurityConstants.RoleId.CONTACT_PERSON,
UserId = newGroup.CreatedById,
GroupId = newGroup.Id,
CreatedById = newGroup.CreatedById
}
});
但是我在 "Add" 上得到一个错误指示器,上面写着 "there is no argument given that corresponds to the required formal parameter 'value' of Dictionary.Add(UserRole,Guid)"
知道我做错了什么吗?
你这样定义你的字典:
Dictionary<UserRole, Guid> _usersRepositoryDictionary = new Dictionary<UserRole, Guid>();
然后像这样添加:
_usersRepositoryDictionary.Add(new[] { new UserRole
{
Id = Guid.NewGuid(),
UserId = newGroup.CreatedById,
RoleId = SecurityConstants.RoleId.GROUP_ADMINISTRATOR,
GroupId = newGroup.Id,
CreatedById = newGroup.CreatedById
},
new UserRole
{
Id = Guid.NewGuid(),
RoleId = SecurityConstants.RoleId.CONTACT_PERSON,
UserId = newGroup.CreatedById,
GroupId = newGroup.Id,
CreatedById = newGroup.CreatedById
}
});
你正在向它添加 dict.Add(new UserRole[] {...}) 当你应该添加 dict.Add (UserRole, Guid);
你需要这样的东西:
var userRole = new UserRole
{
Id = Guid.NewGuid(),
UserId = newGroup.CreatedById,
RoleId = SecurityConstants.RoleId.GROUP_ADMINISTRATOR,
GroupId = newGroup.Id,
CreatedById = newGroup.CreatedById
};
_usersRepositoryDictionary.Add(userRole, userRole.Id);
现在很明显,Id 信息几乎是多余的,所以我建议更好地抽象您的数据模型(例如,继承 IEntity 的东西具有 Id 属性)。
我有以下代码,想将它添加到键值对 List/Array/Dictionary 中,以效果最好的为准。
一点背景: 目前,这个片段直接与数据库交互,它在一个循环中,在每次迭代时将数据推送到数据库。我的总体意图是将此数据推送到 list/dictionary 等,以便我可以一次将所有数据推送到数据库。这是:
_dataContext.GetRepository<UserRole, Guid>().Insert(new[]
{
new UserRole
{
Id = Guid.NewGuid(),
UserId = newGroup.CreatedById,
RoleId = SecurityConstants.RoleId.GROUP_ADMINISTRATOR,
GroupId = newGroup.Id,
CreatedById = newGroup.CreatedById
},
new UserRole
{
Id = Guid.NewGuid(),
RoleId = SecurityConstants.RoleId.CONTACT_PERSON,
UserId = newGroup.CreatedById,
GroupId = newGroup.Id,
CreatedById = newGroup.CreatedById
}
});
上面的 GetRepository 然后访问数据库,如上所述。我想以任何可行的方式单独存储数据,以便稍后将其推送到数据库
public interface IMydDataContext : IDisposable
{
IRepository<TEntity, TKrey> GetRepository<TEntity, TKrey>() where TEntity : class, IDomainObject<TKrey>;
IDbSet<TEntity> Set<TEntity, TKey>() where TEntity : class, IDomainObject<TKey>;
int SaveChanges();
Task<int> SaveChangesAsync();
}
我在这里声明字典:
Dictionary<UserRole, Guid> _usersRepositoryDictionary = new Dictionary<UserRole, Guid>();
并尝试以这种方式将数据放入其中:
_usersRepositoryDictionary.Add(new[] { new UserRole
{
Id = Guid.NewGuid(),
UserId = newGroup.CreatedById,
RoleId = SecurityConstants.RoleId.GROUP_ADMINISTRATOR,
GroupId = newGroup.Id,
CreatedById = newGroup.CreatedById
},
new UserRole
{
Id = Guid.NewGuid(),
RoleId = SecurityConstants.RoleId.CONTACT_PERSON,
UserId = newGroup.CreatedById,
GroupId = newGroup.Id,
CreatedById = newGroup.CreatedById
}
});
但是我在 "Add" 上得到一个错误指示器,上面写着 "there is no argument given that corresponds to the required formal parameter 'value' of Dictionary.Add(UserRole,Guid)"
知道我做错了什么吗?
你这样定义你的字典:
Dictionary<UserRole, Guid> _usersRepositoryDictionary = new Dictionary<UserRole, Guid>();
然后像这样添加:
_usersRepositoryDictionary.Add(new[] { new UserRole
{
Id = Guid.NewGuid(),
UserId = newGroup.CreatedById,
RoleId = SecurityConstants.RoleId.GROUP_ADMINISTRATOR,
GroupId = newGroup.Id,
CreatedById = newGroup.CreatedById
},
new UserRole
{
Id = Guid.NewGuid(),
RoleId = SecurityConstants.RoleId.CONTACT_PERSON,
UserId = newGroup.CreatedById,
GroupId = newGroup.Id,
CreatedById = newGroup.CreatedById
}
});
你正在向它添加 dict.Add(new UserRole[] {...}) 当你应该添加 dict.Add (UserRole, Guid);
你需要这样的东西:
var userRole = new UserRole
{
Id = Guid.NewGuid(),
UserId = newGroup.CreatedById,
RoleId = SecurityConstants.RoleId.GROUP_ADMINISTRATOR,
GroupId = newGroup.Id,
CreatedById = newGroup.CreatedById
};
_usersRepositoryDictionary.Add(userRole, userRole.Id);
现在很明显,Id 信息几乎是多余的,所以我建议更好地抽象您的数据模型(例如,继承 IEntity 的东西具有 Id 属性)。