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

asp.net c# entity-framework linq

Question

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

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

Error: Cannot implicitly convert type System.Collections.Generic.List<AnonymousType#1> to System.Collections.Generic.List<WebApplication2.Customer>

Why is that?

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

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

public object GeAllCust()
{
       var results = db.Customers.Select(x => new { x.CustName, x.CustEmail, x.CustAddress, x.CustContactNo }).ToList();
        return results;
}
1
14
3/26/2017 9:20:17 PM

Accepted Answer

When you look the 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();

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

Edit

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.

21
2/23/2018 3:52:53 PM

Popular Answer

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<ShrunkenCustomer> (assuming ShunkenCustomer is the name that you choose for your new class.)


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow