The entity cannot be constructed in a LINQ to Entities query

前端 未结 14 2033
失恋的感觉
失恋的感觉 2020-11-21 06:04

There is an entity type called Product that is generated by entity framework. I have written this query

public IQueryable GetProdu         


        
14条回答
  •  一生所求
    2020-11-21 06:37

    It won't let you map back onto Product since that is your table you are querying. You need an anonymous function, then you can add it to a ViewModel, and add each ViewModel to a List and return these. It's a slight digression, but I include caveats about handling nullable dates because these are a pain in the behind to deal with, just in case you have any. This is how I handled it.

    Hopefully you have a ProductViewModel:

    public class ProductViewModel
    {
        [Key]
        public string ID { get; set; }
        public string Name { get; set; }
    }
    

    I have dependency injection/repository framework where I call a function to grab my data. Using your post as an example, in your Controller function call, it would look like this:

    int categoryID = 1;
    var prods = repository.GetProducts(categoryID);
    

    In the repository class:

    public IEnumerable GetProducts(int categoryID)
    {
       List lstPVM = new List();
    
       var anonymousObjResult = from p in db.Products
                                where p.CategoryID == categoryID 
                                select new
                                {
                                    CatID = p.CategoryID,
                                    Name = p.Name
                                };
    
            // NOTE: If you have any dates that are nullable and null, you'll need to
            // take care of that:  ClosedDate = (DateTime?)p.ClosedDate ?? DateTime.Now
    
            // If you want a particular date, you have to define a DateTime variable,
            // assign your value to it, then replace DateTime.Now with that variable. You
            // cannot call a DateTime.Parse there, unfortunately. 
            // Using 
            //    new Date("1","1","1800"); 
            // works, though. (I add a particular date so I can edit it out later.)
    
            // I do this foreach below so I can return a List. 
            // You could do: return anonymousObjResult.ToList(); here
            // but it's not as clean and is an anonymous type instead of defined
            // by a ViewModel where you can control the individual field types
    
            foreach (var a in anonymousObjResult)
            {                
                ProductViewModel pvm = new ProductViewModel();
                pvm.ID = a.CatID;  
                pvm.Name = a.Name;
                lstPVM.Add(rvm);
            }
    
            // Obviously you will just have ONE item there, but I built it 
            // like this so you could bring back the whole table, if you wanted
            // to remove your Where clause, above.
    
            return lstPVM;
        }
    

    Back in the controller, you do:

     List lstProd = new List();
    
     if (prods != null) 
     {
        // For setting the dates back to nulls, I'm looking for this value:
        // DateTime stdDate = DateTime.Parse("01/01/1800");
    
        foreach (var a in prods)
        {
            ProductViewModel o_prod = new ReportViewModel();
            o_prod.ID = a.ID;
            o_prod.Name = a.Name;
           // o_prod.ClosedDate = a.ClosedDate == stdDate ? null : a.ClosedDate;
            lstProd.Add(o_prod);
        }
    }
    return View(lstProd);  // use this in your View as:   @model IEnumerable
    

提交回复
热议问题