LINQ to Entities does not support the provided type member. Initializers, entity members, and entity navigation properties are the only things that work.

asp.net-mvc-3 entity-framework grid linq

Question

The previous two days have been spent trying to figure out why this isn't functioning yet it is for my other tables. Even my other model, which has a lot more fields, was evaluated. However, this one does not function even with just 2 fields. Please enlighten me if I could be missing a simple component.

This is my model.

public class ReceivedItem
{        
    public int ReceivedItemID { get; set; }
    public int ItemID { get; set; }
}

ViewModel

public class ReceivedItemViewModel
 {
    public int ReceivedItemID { get; set; }
    public int ItemID { get; set; }
 }

Controller

[GridAction]
public ActionResult GetReceivedItems()
  {
      return View(new GridModel(GetReceivedItemsViewModels()));
  }

private IQueryable<ReceivedItemViewModel> GetReceivedItemsViewModels()
{
    return db.ReceivedItems
         .Select(
          c => new ReceivedItemViewModel
            { 
               ItemID = c.ItemID
             });
 }

View

 @(Html.Telerik().Grid<ReceivedItem>()
.Name("grdItems")
.DataBinding(binding => binding.Ajax()
    .Select("GetReceivedItems", "Receiving"))
.DataKeys(keys => keys.Add(o => o.ItemID))
.Columns(cols =>
{  
    cols.Bound(c => c.ItemID);
})
.Pageable()
.Sortable()
.Groupable()
.Filterable()

)

Firebug error that I encountered:

The specified type member 'ReceivedItemID' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
1
9
4/15/2012 4:35:49 AM

Accepted Answer

As far as I'm aware, a Linq-to-SQL query cannot instantiate non-entity objects. Try counting the outcomes before using Linq to build your view models.

What you have (I assume this is the location where the exception is being thrown):

// Original Code
return db.ReceivedItems
     .Select(
      c => new ReceivedItemViewModel
        { 
           ItemID = c.ItemID
         });

Using Linq to construct your view models after enumerating

// First statement
var items = db.ReceivedItems.ToArray(); // Enumerates the collection.
// Second statement
return items.Select(
  c => new ReceivedItemViewModel
    { 
       ItemID = c.ItemID
     });

The results of the first Linq statement are converted into a SQL query, returned, and enumerated as an array. The array is then utilized to generate the collection of view models in the second step. In your initial statement, the view model would have to be taken into consideration during the SQL translation of the Linq statement (which it is unable to do).

Hope this was helpful. (And is logical)

12
6/1/2012 5:57:36 PM

Popular Answer

Given that I just had this problem when utilizing a Telerik Kendo Grid, I think the root of the problem lies with Telerik. The exception was not really caused by projecting to a view model (inside the select).

Keep in mind that the projection does not map the "ReceivedItemID" referenced column. Simply assigning the missing attribute solved my problem.

return db.ReceivedItems
     .Select(
      c => new ReceivedItemViewModel
        { 
           ReceivedItemID = c.ReceivedItemID
           ItemID = c.ItemID
         });

In my situation, the model was also going through the following extra step:

return Json(model.ToDataSourceResult(request));

The SQL query was becoming complicated because of how the Telerik ToDataSourceRequest extension function alters the SQL query.



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