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

标记

我想用DateTime属性以及类别列表填充viewmodel。

ViewModel:

public class TourCategoryVM
{
    public DateTime Date { get; set; }
    public List<TourCategoryList> TourCategoryList { get; set; }
}

public class TourCategoryList
{
    public int TourCategoryId { get; set; }
    public string TourType { get; set; }
}

域模型:

public class TourCategory
{
    public int TourCategoryId { get; set; }
    public string TourType { get; set; }
    public virtual ICollection<Tour> Tour { get; set; }
}

我以为可以用以下代码轻松填充它:

        var viewModel = new TourCategoryVM();
        viewModel.TourCategoryList = db.TourCategories();

但是,我得到了错误:

错误1无法将类型隐式转换
System.Data.Entity.DbSet<tb.Models.TourCategory>
System.Collections.Generic.List<tb.Models.ViewModels.TourCategoryList>

是我的ViewModel错误吗?

格兰特·温尼

db.TourCategories()方法不会返回的集合TourCategoryList,因此您需要做更多的工作才能使用LINQ方法TourCategories()返回的任何类转换TourCategoryListSelect()

viewModel.TourCategoryList = db.TourCategories()
                               .Select(tc => new TourCategoryList
                                             {
                                                 TourCategoryId = tc.TourCategoryId,
                                                 TourType = tc.TourType
                                             })
                               .ToList();

我假设这会TourCategories()传回的集合TourCategory


如果我可以提出其他建议,则可能要重命名TourCategoryList我知道您正在尝试将其与其他TourCategory类区分开,但是看您的代码的人(乍看之下)可能会认为List<TourCategoryList>只是列表列表,只是名字而已

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类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

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

无法将类型“ void”隐式转换为“ System.Collections.Generic.List”以用于返回语句

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

无法将类型“字符串”隐式转换为“System.Collections.Generic.List<int>”

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

Related 相关文章

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

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

  5. 5

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

  6. 6

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

  7. 7

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

  8. 8

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

  9. 9

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

  10. 10

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

  11. 11

    无法将类型“ void”隐式转换为“ System.Collections.Generic.List”以用于返回语句

  12. 12

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

  13. 13

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

  14. 14

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

  15. 15

    无法将类型“字符串”隐式转换为“System.Collections.Generic.List<int>”

  16. 16

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

  17. 17

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

  18. 18

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

  19. 19

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

  20. 20

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

  21. 21

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

  22. 22

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

  23. 23

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

  24. 24

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

  25. 25

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

  26. 26

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

  27. 27

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

  28. 28

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

  29. 29

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

热门标签

归档