无法将类型'.List <AnonymousType#1>'隐式转换为'.List <WebApplication2.Customer>'

阿卜杜勒·哈利克(Abdul Khaliq)

在下面的代码中返回列表:

public List<Customer> GeAllCust()
{
    var results = db.Customers
        .Select(x => new { x.CustName, x.CustEmail, x.CustAddress, x.CustContactNo })
        .ToList()
    return results;
}

我收到一个错误报告,指出C#无法转换列表:

错误:无法将类型隐式转换System.Collections.Generic.List<AnonymousType#1>System.Collections.Generic.List<WebApplication2.Customer>

这是为什么?

这是一个屏幕快照,显示了Visual Studio在错误提示中提供的一些其他信息:

返回某些列而不是整个表的正确方法是...吗?

public object GeAllCust()
{
       var results = db.Customers.Select(x => new { x.CustName, x.CustEmail, x.CustAddress, x.CustContactNo }).ToList();
        return results;
}
枪手2171

当您查看代码时:

x => new { ... }

这将创建一个新的匿名类型如果您不需要仅拉回一组特定的列,则可以执行以下操作:

return db.Customers.ToList();

假设这Customers是个IEnumerable<Customer>,应与您要返回的内容相匹配。

编辑

您已经注意到,您只想返回某些列的子集。如果在编码时需要任何类型的编译器帮助,则需要创建一个自定义类来保存值:

public class CustomerMinInfo
{
    public string Name { get; set; }
    public string Email { get; set; }
    public string Address { get; set; }
    public int? ContactNumber { get; set; }
}

然后将您的功能更改为以下内容:

public List<CustomerMinInfo> GetAllCust()
{
    var results = db.Customers.Select(x => new CustomerMinInfo()
    {
        Name = x.CustName,
        Email = x.Email,
        Address = x.Address,
        ContactNumber = x.CustContactNo
    })
    .ToList();

    return results;
}

这将起作用,但是,您将失去与数据库上下文的所有关系。这意味着,如果您更新返回的值,则不会将其重新粘贴到数据库中。

同样,仅重复我的评论,返回更多列(字节数组除外)并不一定意味着更长的执行时间。返回很多行意味着更多的执行时间。您的函数将返回数据库中的每个客户,随着系统的增长,即使列数减少,该客户也将开始挂起程序。

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

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

C# 不能隐式转换类型“System.Collections.Generic.List<AnonymousType#1>”

来自分类Dev

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

来自分类Dev

无法将类型System.Linq.IQueryable <AnonymousType#1>隐式转换为System.Linq.IQueryable <AnonymousType#2>

来自分类Dev

2错误-无法将类型system.collection.generic.list <float>隐式转换为float,也无法将float []类型隐式转换为float

来自分类Dev

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

来自分类Dev

无法从“ AnonymousType#1”转换为“ int”

来自分类Dev

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

来自分类Dev

无法将类型System.Collections.Generic.Ienumerable <string>隐式转换为system.Linq.IOrderedEnumerable <AnonymousType#1>

来自分类Dev

无法将类型List <Class>隐式转换为List <OtherClass>错误

来自分类Dev

无法将类型 List<SchoolFee.FeeCollection> 隐式转换为 List<Student.FeeCollection> 错误

来自分类Dev

无法将类型 Generic.List 隐式转换为 Model 类型

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

无法使用JavascriptSerializer隐式将类型字符串转换为List

来自分类Dev

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

来自分类Dev

无法将类型“IEnumerable<ob>”隐式转换为“Generic.List<cdsworkflows>”嵌套对象

来自分类Dev

无法将类型“System.Collection.Generic.List<T>”隐式转换为“T”

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

Related 相关文章

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

    C# 不能隐式转换类型“System.Collections.Generic.List<AnonymousType#1>”

  5. 5

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

  6. 6

    无法将类型System.Linq.IQueryable <AnonymousType#1>隐式转换为System.Linq.IQueryable <AnonymousType#2>

  7. 7

    2错误-无法将类型system.collection.generic.list <float>隐式转换为float,也无法将float []类型隐式转换为float

  8. 8

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

  9. 9

    无法从“ AnonymousType#1”转换为“ int”

  10. 10

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

  11. 11

    无法将类型System.Collections.Generic.Ienumerable <string>隐式转换为system.Linq.IOrderedEnumerable <AnonymousType#1>

  12. 12

    无法将类型List <Class>隐式转换为List <OtherClass>错误

  13. 13

    无法将类型 List<SchoolFee.FeeCollection> 隐式转换为 List<Student.FeeCollection> 错误

  14. 14

    无法将类型 Generic.List 隐式转换为 Model 类型

  15. 15

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

  16. 16

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

  17. 17

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

  18. 18

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

  19. 19

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

  20. 20

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

  21. 21

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

  22. 22

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

  23. 23

    无法使用JavascriptSerializer隐式将类型字符串转换为List

  24. 24

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

  25. 25

    无法将类型“IEnumerable<ob>”隐式转换为“Generic.List<cdsworkflows>”嵌套对象

  26. 26

    无法将类型“System.Collection.Generic.List<T>”隐式转换为“T”

  27. 27

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

  28. 28

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

  29. 29

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

热门标签

归档