将类型'System.Collections.Generic.List <string>'转换为'System.Collections.Generic.IList <>'

mohsinali1317

在我的模型中,Account我有一个像这样的属性

public List<String> Roles { get; set; }

稍后我想获取该属性但将其转换IList<IApplicationUserRole<Role>>,所以我有此功能

 public IList<IApplicationUserRole<Role>> Roles
    {
        get
        {
            return _account.Roles; // how do I convert this in the specific type intended. 
        }
    }

这是我的IApplicationUserRole

public interface IApplicationUserRole<TRoleModel> : IRole<string>
    where TRoleModel : EntityModel
{
    TRoleModel Role { get; set; }

    String Name { get; set; }
}

我是这个东西的新手。期待任何帮助。

候选者

假设您的实现类如下:

public class ApplicationUserRole : IApplicationUserRole<T> where T : Role
{
    public ApplicationUserRole()
    {
    }
    public User User { get; set; }
    public T Role { get; set; }
}

然后,您将执行以下操作:

public IList<IApplicationUserRole<Role>> Roles
{
    get
    {
        return _account.Roles
            .Select(r => new ApplicationUserRole { Role = roleService.FindRoleByName(r) })
            .Cast<IApplicationUserRole<Role>>()
            .ToList(); 
    }
}

从角色名称roleService构建Role实例的方式在哪里(上面是r

注意:这么说,上面的实现有一个陷阱由于Roles是属性,因此不应执行数据访问操作。因此,在这种情况下,您应该创建一个方法而不是一个属性。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法将类型'System.Collections.IList'隐式转换为'System.Collections.Generic.List

来自分类Dev

无法将类型'System.Collections.Generic.List <>'隐式转换为'System.Collections.Generic.IList <>'

来自分类Dev

无法将类型'System.Collections.Generic.List <>'转换为'System.Collections.Generic.IList <>'c#MongoDB

来自分类Dev

无法将类型'System.Collections.Generic.List <>'隐式转换为'System.Collections.Generic.IList <>'

来自分类Dev

无法将类型'System.Collections.Generic.List <string>'隐式转换为'System.Collections.Generic.IList <LM_WebApi.Controllers.Items>

来自分类Dev

无法将类型'System.Collections.Generic.List <string>'转换为'System.Web.Mvc.SelectList'

来自分类Dev

无法将'System.Collections.Generic.List <long?>'转换为'System.Collections.Generic.List <long>

来自分类Dev

无法将'System.Collections.Generic.List <long?>'转换为'System.Collections.Generic.List <long>

来自分类Dev

无法将类型'void'隐式转换为'System.Collections.Generic.List <string>'

来自分类Dev

无法将类型'System.Collections.Generic.List'隐式转换为'string'

来自分类Dev

无法将类型“System.Collections.Generic.List<Object>”隐式转换为“string”

来自分类Dev

无法将类型“System.Collections.Generic.List<<匿名类型:>>”隐式转换为“System.Collections.Generic.List”

来自分类Dev

无法将类型'System.Collections.Generic.List <System.Data.DataRow>'隐式转换为'System.Collections.Generic.List <string>'

来自分类Dev

将'System.Collections.Generic.IEnumerable <string>'转换为'string []

来自分类Dev

无法将类型'System.Collections.Generic.List <AnonymousType#1>'隐式转换为'System.Collections.Generic.List

来自分类Dev

无法将类型System.Collections.Generic.List <>隐式转换为System.Collections.Generic.List <>

来自分类Dev

无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List<Model.Room”

来自分类Dev

无法将类型'System.Collections.Generic.List <AnonymousType#1>'隐式转换为'System.Collections.Generic.List <string>'

来自分类Dev

错误“无法将类型“System.Collections.Generic.IEnumerable<int>”隐式转换为“System.Collections.Generic.List<int>”

来自分类Dev

无法将类型'System.Collections.Generic.List <String>'隐式转换为'System.Collections.Generic.IEnumerable <turon.Model.Products_Products>

来自分类Dev

无法将类型'System.Linq.IQueryable'隐式转换为'System.Collections.Generic.List'

来自分类Dev

无法将类型'System.Linq.IQueryable'隐式转换为'System.Collections.Generic.List

来自分类Dev

无法将类型'System.Collections.Generic.List <>'隐式转换为'IList <>'。存在显式转换(您是否缺少演员表?)

来自分类Dev

JSON Restful服务。无法将System.String强制转换或转换为System.Collections.Generic.List

来自分类Dev

无法从System.Collections.Generic.List转换为System.Collections.Generic.IEnumerable

来自分类Dev

无法将类型隐式转换为'System.Collections.Generic.List

来自分类Dev

将值转换为我的JSON类型'System.Collections.Generic.List`时出错

来自分类Dev

无法将类型System.Collections.Generic.List <IEnumerable>隐式转换为<IEnumerable

来自分类Dev

无法将类型'int'隐式转换为'System.Collections.Generic.List <QuickTest.Stock>'

Related 相关文章

  1. 1

    无法将类型'System.Collections.IList'隐式转换为'System.Collections.Generic.List

  2. 2

    无法将类型'System.Collections.Generic.List <>'隐式转换为'System.Collections.Generic.IList <>'

  3. 3

    无法将类型'System.Collections.Generic.List <>'转换为'System.Collections.Generic.IList <>'c#MongoDB

  4. 4

    无法将类型'System.Collections.Generic.List <>'隐式转换为'System.Collections.Generic.IList <>'

  5. 5

    无法将类型'System.Collections.Generic.List <string>'隐式转换为'System.Collections.Generic.IList <LM_WebApi.Controllers.Items>

  6. 6

    无法将类型'System.Collections.Generic.List <string>'转换为'System.Web.Mvc.SelectList'

  7. 7

    无法将'System.Collections.Generic.List <long?>'转换为'System.Collections.Generic.List <long>

  8. 8

    无法将'System.Collections.Generic.List <long?>'转换为'System.Collections.Generic.List <long>

  9. 9

    无法将类型'void'隐式转换为'System.Collections.Generic.List <string>'

  10. 10

    无法将类型'System.Collections.Generic.List'隐式转换为'string'

  11. 11

    无法将类型“System.Collections.Generic.List<Object>”隐式转换为“string”

  12. 12

    无法将类型“System.Collections.Generic.List<<匿名类型:>>”隐式转换为“System.Collections.Generic.List”

  13. 13

    无法将类型'System.Collections.Generic.List <System.Data.DataRow>'隐式转换为'System.Collections.Generic.List <string>'

  14. 14

    将'System.Collections.Generic.IEnumerable <string>'转换为'string []

  15. 15

    无法将类型'System.Collections.Generic.List <AnonymousType#1>'隐式转换为'System.Collections.Generic.List

  16. 16

    无法将类型System.Collections.Generic.List <>隐式转换为System.Collections.Generic.List <>

  17. 17

    无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List<Model.Room”

  18. 18

    无法将类型'System.Collections.Generic.List <AnonymousType#1>'隐式转换为'System.Collections.Generic.List <string>'

  19. 19

    错误“无法将类型“System.Collections.Generic.IEnumerable<int>”隐式转换为“System.Collections.Generic.List<int>”

  20. 20

    无法将类型'System.Collections.Generic.List <String>'隐式转换为'System.Collections.Generic.IEnumerable <turon.Model.Products_Products>

  21. 21

    无法将类型'System.Linq.IQueryable'隐式转换为'System.Collections.Generic.List'

  22. 22

    无法将类型'System.Linq.IQueryable'隐式转换为'System.Collections.Generic.List

  23. 23

    无法将类型'System.Collections.Generic.List <>'隐式转换为'IList <>'。存在显式转换(您是否缺少演员表?)

  24. 24

    JSON Restful服务。无法将System.String强制转换或转换为System.Collections.Generic.List

  25. 25

    无法从System.Collections.Generic.List转换为System.Collections.Generic.IEnumerable

  26. 26

    无法将类型隐式转换为'System.Collections.Generic.List

  27. 27

    将值转换为我的JSON类型'System.Collections.Generic.List`时出错

  28. 28

    无法将类型System.Collections.Generic.List <IEnumerable>隐式转换为<IEnumerable

  29. 29

    无法将类型'int'隐式转换为'System.Collections.Generic.List <QuickTest.Stock>'

热门标签

归档