11
votes

I'm trying to populate a dropdown in my view. Any help is greatly appreciated. Thanks.

Error:

Unable to cast the type 'System.Int32' to type 'System.Object'.

LINQ to Entities only supports casting Entity Data Model primitive types.

Controller:

ViewBag.category = (from c in new IntraEntities().CategoryItems
                   select new SelectListItem() {Text=c.Name, Value=""+c.ID }).ToList<SelectListItem>();

View:

Category:<br />@Html.DropDownList("category", (List<SelectListItem>)ViewBag.category)
6
Linq to enitites does not support these types of conversions convert.Tostring() and convert.ToDatetime().. - Enigma State

6 Answers

20
votes

How about this:

ViewBag.category = 
    from c in new IntraEntities().CategoryItems.ToList()
    select new SelectListItem 
    {
        Text = c.Name, 
        Value = c.ID.ToString() 
    };

and how about using strongly typed view models instead of some weakly typed crap of a ViewBag (it's the way I call it)?

Like this:

public class CategoryViewModel
{
    public string CategoryId { get; set; }
    public IEnumerable<SelectListItem> Categories { get; set; }
}

then:

public ActionResult Foo()
{
    var model = new CategoryViewModel
    {
        Categories = 
            from c in new IntraEntities().CategoryItems.ToList()
            select new SelectListItem 
            {
                Text = c.Name, 
                Value = c.ID.ToString() 
            }
    };
    return View(model);
}

and finally in your strongly typed view:

@model CategoryViewModel
@using (Html.BeginForm())
{
    @Html.DropDownListFor(x => x.CategoryId, Model.Categories)
    <button type="submit">OK</button>
}

Much better, don't you think?

5
votes

You can cast your query to an .AsEnumerable() prior to the conversion to force it to use Linq to Objects for the casting but it is better to use the SQL compatible functions available in System.Data.Objects.SqlClient.SqlFunctions like so:

(from c in new IntraEntities().CategoryItems
select new SelectListItem() { 
    Text = c.Name, 
    Value = SqlFunctions.StringConvert((double)c.ID).TrimStart() 
})
0
votes
ViewBag.category = (from c in new IntraEntities().CategoryItems
                   select new SelectListItem {Text=c.Name, Value=c.ID.ToString()})
.ToList<SelectListItem>();
0
votes

The Error says, you are trying to concat string with integer value. It's not possible in linq query. For Convert int into string you can use

SqlFunctions.StringConvert((decimal?)intProperty) 
0
votes

you can use the SQL functions to convert like this:

ViewBag.category = (from c in new IntraEntities().CategoryItems
                   select new SelectListItem() {
Text=c.Name, 
Value=SqlFunctions.StringConvert((double) c.ID) }).ToList<SelectListItem>();

but do not forget to include this namespace at the top of your file:

using System.Data.Objects.SqlClient;

0
votes

Yes you are trying to " to concat string with integer value. It's not possible in linq query" just as Nalan M suggested. Make sure it is the column in the database that you are trying to get. For me that is what it was. I was specifying the wrong column. I was calling the the class because apparently someone made a class for that property. If you are working in visual studio, you can check the database and see what it is that you are calling. That way you can answer the question : "where is the column I want?" in the database.