Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot implicitly convert type '.List<AnonymousType#1>' to '.List<WebApplication2.Customer>'

Tags:

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;
}
like image 349
Abdul Khaliq Avatar asked Jul 24 '13 14:07

Abdul Khaliq


People also ask

How do I change my class type to Anonymous?

Convert anonymous type(s) into a named typePress Ctrl+Shift+R and then choose Replace Anonymous Type with Named Class.

What is anonymous type in LINQ?

Anonymous types provide a convenient way to encapsulate a set of read-only properties in an object without having to explicitly define a type first. If you write a query that creates an object of an anonymous type in the select clause, the query returns an IEnumerable of the type.


4 Answers

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.

like image 170
gunr2171 Avatar answered Oct 01 '22 16:10

gunr2171


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.)
like image 24
Mark Avenius Avatar answered Oct 01 '22 18:10

Mark Avenius


GetAllCust() is supposed to return a List of Customer, Select New will create a list of Anonymous Types, you need to return a list of Customer from your query. try:

var results = db.Customers.Select( new Customer{CustName = x.CustName}).ToList(); //include other fields
like image 44
robasta Avatar answered Oct 01 '22 17:10

robasta


I guess Customer is a class you have defined yourself? The my suggestion would be to do something like the following:

var results = db.Customers.Select(x => new Customer(x.Custname, x.CustEmail, x.CustAddress, x.CustContactNo)).ToList();

The reason is that you are trying to return a list of Customer but the results from your link is an anonymous class containing those four values. This would of course require that you have a constructor that takes those four values.

like image 43
kumaheiyama Avatar answered Oct 01 '22 17:10

kumaheiyama