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

后端 未结 5 1033
隐瞒了意图╮
隐瞒了意图╮ 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:20

    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
    
    0 讨论(0)
  • 2020-12-28 19:24

    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.

    0 讨论(0)
  • 2020-12-28 19:28

    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.

    0 讨论(0)
  • 2020-12-28 19:34

    Basically whatever u got in var type, loop on that and store it in list<> object then loop and achieve ur target.Here I m posting code for Master details. List obj = new List();

            var orderlist = (from a in db.Order_Master
                             join b in db.UserAccounts on a.User_Id equals b.Id into abc
                             from b in abc.DefaultIfEmpty()
                             select new
                             {
                                 Order_Id = a.Order_Id,
                                 User_Name = b.FirstName,
                                 Order_Date = a.Order_Date,
                                 Tot_Qty = a.Tot_Qty,
                                 Tot_Price = a.Tot_Price,     
                                 Order_Status = a.Order_Status,
                                 Payment_Mode = a.Payment_Mode,
                                 Address_Id = a.Address_Id 
    
                             });
    
    
            List<MasterOrder> ob = new List<MasterOrder>();
    
            foreach (var item in orderlist)
            {
    
                MasterOrder clr = new MasterOrder();
                clr.Order_Id = item.Order_Id;
                clr.User_Name = item.User_Name;
                clr.Order_Date = item.Order_Date;
                clr.Tot_Qty = item.Tot_Qty;
                clr.Tot_Price = item.Tot_Price;
                clr.Order_Status = item.Order_Status;       
                clr.Payment_Mode = item.Payment_Mode;
                clr.Address_Id = item.Address_Id;              
                ob.Add(clr);
    
    
            }
    
    
    
            using(ecom_storeEntities en=new ecom_storeEntities())
            {
                var Masterlist = en.Order_Master.OrderByDescending(a => a.Order_Id).ToList();
    
    
                foreach (var i in ob)
                {
                    var Child = en.Order_Child.Where(a => a.Order_Id==i.Order_Id).ToList();
                    obj.Add(new OrderMasterChild 
                    {
                        Master = i,
                        Childs = Child
                    });
                }        
    
    
            }
    
    0 讨论(0)
  • 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<ShrunkenCustomer> (assuming ShunkenCustomer is the name that you choose for your new class.)
    0 讨论(0)
提交回复
热议问题