无法隐式转换类型'的.List< AnonymousType#1>'为“的.List< WebApplication2.Customer>' [英] Cannot implicitly convert type '.List<AnonymousType#1>' to '.List<WebApplication2.Customer>'

查看:177
本文介绍了无法隐式转换类型'的.List< AnonymousType#1>'为“的.List< WebApplication2.Customer>'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

块引用

在以下code,它返回一个列表:

In the following code that returns a list:

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

我得到一个错误报告说,C#不能转换列表:

I get an error reporting that C# can't convert the list:

错误:无法隐式转换类型'System.Collections.Generic.List'到'System.Collections.Generic.List'。

"Error: Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List'"

这是为什么?

下面是可见的Visual Studio中的错误的工具提示提供了一些附加信息的截图:

Here's a screenshot showing some additional information that Visual Studio provides in a tooltip for the error:

它是返回一些coloms,而不是整个表的正确的方式....?

Is it right way to return some coloms instead of whole table....?

公共对象GeAllCust()
        {
           VAR的结果= db.Customers.Select(X =>新{x.Cu​​stName,x.CustEmail,x.CustAddress,x.CustContactNo})了ToList()。
            返回结果;
        }块引用

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

推荐答案

当你看code:

x => new { ... }

这将创建一个新的匿名类型。如果你不需要拉回只有一组特定的列,你可以做到以下几点:

This creates a new anonymous type. If you don't need to pull back only a particular set of columns, you can just do the following:

return db.Customers.ToList();

这假定客户的IEnumerable&LT;客户&GT; ,这应该与你试图匹配起来返回。

This assumes that Customers is an IEnumerable<Customer>, which should match up with what you are trying to return.

修改

您已经注意到,你只希望返回列的某个子集。如果你想任何形式的编译器帮助编码这时候,你需要做一个自定义的类来保存的值:

You have noted that you only want to return a certain subset of columns. If you want any sort of compiler help when coding this, you need to make a custom class to hold the values:

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

那么你的功能更改为以下内容:

Then change your function to the following:

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

这将工作,然而后,您将失去对数据库的上下文中的所有关系。这意味着,如果你更新返回的值,也不会粘回到数据库中。

This will work, however, you will lose all relationship to the database context. This means if you update the returned values, it will not stick it back into the database.

此外,只需重复我的评论,返回更多的列(使用字节数组除外)并不一定意味着更长的执行时间。返回很多行意味着更多的执行时间。你的函数返回的每一个客户数据库的,这时候你的系统的发展,将开始挂程序,即使列的减少量。

Also, just to repeat my comment, returning more columns (with the exception of byte arrays) does not necessarily mean longer execution time. Returning a lot of rows means more execution time. Your function is returning every single customer in the database, which when your system grows, will start to hang your program, even with the reduced amount of columns.

这篇关于无法隐式转换类型'的.List&LT; AnonymousType#1&GT;'为“的.List&LT; WebApplication2.Customer&GT;'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆