Dantuzzo Dantuzzo - 1 year ago 210
ASP.NET (C#) Question

Does not contain a definition for 'AsEnumerable' and 'cannot convert from methodgroup' in Razor view

I'm getting an error which I can't find the solution to, it's that my model does not contain a definition for 'AsEnumerable' (I put the whole error lower in the post), I've read about it around Stack Overflow, all answers seem to point towards

using system.Linq
using system.Data
, but I have them in the code.

The program is supposed to take some data from tables: Contatti, Contacts and Companies which are at different locations and bring them together (which happen in the list of the GET method in the controller), the code is the following:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Globalization;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

public partial class ContactsUni2
public IEnumerable<Contatti> Contattis { get; set; }
public IEnumerable<Companies> Companies { get; set; }
public IEnumerable<Contact> Contacts { get; set; }

Controller GET Which Gives the error:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ContactManager.Models;

namespace ContactManager.Controllers
public class ContactsUni21Controller : Controller
private ApplicationDbContext db = new ApplicationDbContext();
private ContattiDB2 db2 = new ContattiDB2();

// GET: ContactsUni21
public ActionResult Index(String Page)
ContactsUni2 CU = new ContactsUni2();
CU.Contattis = db.Contattis.Include(i => i.ContattoID);
CU.Contacts = db.Contacts;
CU.Companies = db.Companies;
List<ContactsUni2> contactlist = new List<ContactsUni2>();
contactlist.Add(CU.AsEnumerable()); // The error is HERE at CU.AsEnumerable(), cannot convert from method group to 'contactsuni2'
return View(contactlist);

View, which also gives a similar error but for Contatti.Count()

@model IEnumerable<ContactManager.ViewModels.ContactsUni3>

ViewBag.Title = "Index";

<h2>Contacts Unified</h2>

<meta name="viewport" content="width=device-width" />
@Html.ActionLink("Create New", "Create")
<table class="table">
@Html.DisplayNameFor(model => model.Contatti.Nome)
@Html.DisplayNameFor(model => model.Contatti.Citta)
@Html.DisplayNameFor(model => model.Contatti.ContattoID)
@Html.DisplayNameFor(model => model.Contatti.CodicePostale)
@Html.DisplayNameFor(model => model.Contatti.Email)
@Html.DisplayNameFor(model => model.Contact.Address)
@Html.DisplayNameFor(model => model.Contact.CompanyId)
@Html.DisplayNameFor(model => model.Contact.ContactId)
@Html.DisplayNameFor(model => model.Company.CompanyName)


@foreach (var item in Model)
for (int i = 0; i < @item.Contatti.Count(); i++) // Second Error Here

@Html.ActionLink("Edit", "Edit", new { id = item.ContattoID }) |
@Html.ActionLink("Details", "Details", new { id = item.ContattoID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ContattoID })


First error in Controller GET Method:

Error CS1929 'ContactsUni2' does not contain a definition for 'AsEnumerable' and the best extension method overload 'DataTableExtensions.AsEnumerable(DataTable)' requires a receiver of type 'DataTable'

when I try doing that, it says:

cannot convert from method group to 'contactsuni2'

Second Error in View loop:

Error CS1061 'Contatti' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'Contatti' could be found (are you missing a using directive or an assembly reference?)

Answer Source

Just use this:


The method List.Add() expects a single item, and CU is a single item.

And to fix the problem in your View: simply add a line with @using System.Linq on top so the compiler will know where to look.

Additional info about AsEnumerable:

You only need AsEnumerable() if you have a collection or sequence of items, but that collection is incompatible with a construct such as foreach (var item in collection).
In that case you can try using foreach (var item in collection.AsEnumerable()).

In some cases the .NET Framework may then already provide the implementation of .AsEnumerable(), in other cases you may have to provide it yourself. But in the case of your question you don't even need it at all.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download