Cannot implicitly convert type '.List' to '.List'

后端 未结 5 1052
隐瞒了意图╮
隐瞒了意图╮ 2020-12-28 19:06

In the following code that returns a list:

public List GeAllCust()
{
    var results = db.Customers
        .Select(x => new { x.CustName,         


        
5条回答
  •  庸人自扰
    2020-12-28 19:47

    You are selecting to an anonymous type, which is not a Customer.

    If you want to do (sort of) this, you can write it like this:

    return db.Customers.Select(x => new Customer { Name = x.CustName, Email = x.CustEmail, Address = x.CustAddress, ContactNo = x.ContactNo }).ToList();
    

    This assumes the properties on your Customer object are what I called them.

    ** EDIT ** Per your comment,

    If you want to return a subset of the table, you can do one of two things:

    1. Return the translated form of Customer as I specified above, or:
    2. Create a new class for your business layer that only has only those four fields, and change your method to return a List (assuming ShunkenCustomer is the name that you choose for your new class.)

提交回复
热议问题