Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不包含';可计算的';和';无法从methodgroup';在锐利的视野中_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 不包含';可计算的';和';无法从methodgroup';在锐利的视野中

C# 不包含';可计算的';和';无法从methodgroup';在锐利的视野中,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我遇到了一个我找不到解决方案的错误,那就是我的模型没有包含“可计算”的定义(我把整个错误放在后面),我读过关于堆栈溢出的文章,所有的答案似乎都指向使用system.Linq的和使用system.Data的,但我在代码中有它们 该程序应该从表中获取一些数据:Contatti、联系人和位于不同位置的公司,并将它们放在一起(发生在控制器中GET方法的列表中),代码如下: 型号: using System; using System.Collections.Generic; using System.L

我遇到了一个我找不到解决方案的错误,那就是我的模型没有包含“可计算”的定义(我把整个错误放在后面),我读过关于堆栈溢出的文章,所有的答案似乎都指向使用system.Linq的
和使用system.Data的
,但我在代码中有它们

该程序应该从表中获取一些数据:Contatti、联系人和位于不同位置的公司,并将它们放在一起(发生在控制器中GET方法的列表中),代码如下:

型号:

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; }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Data.Entity;
使用System.Data.Entity.Infrastructure;
利用制度全球化;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
公共部分类联系人SUNI2
{
公共IEnumerable Contattis{get;set;}
公共IEnumerable公司{get;set;}
公共IEnumerable联系人{get;set;}
}
给出错误的控制器GET:

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);
    }
使用系统;
使用System.Collections.Generic;
使用系统数据;
使用System.Data.Entity;
使用System.Linq;
Net系统;
使用System.Web;
使用System.Web.Mvc;
使用ContactManager.Models;
名称空间ContactManager.Controllers
{
公共类联系人SUNI21控制器:控制器
{
私有ApplicationDbContext db=新ApplicationDbContext();
私有ContattiDB2=新ContattiDB2();
//获取:ContactsUni21
公共操作结果索引(字符串页)
{
ContactsUni2 CU=新的ContactsUni2();
CU.Contattis=db.Contattis.Include(i=>i.ContattoID);
铜触点=分贝触点;
CU.公司=db.公司;
列表联系人列表=新列表();
contactlist.Add(CU.AsEnumerable());//错误位于CU.AsEnumerable(),无法从方法组转换为“contactsuni2”
返回视图(联系人列表);
}
视图,它也给出了一个类似的错误,但对于Contatti.Count()

@model IEnumerable
@{
ViewBag.Title=“Index”;
}
联系人统一
指数

@ActionLink(“新建”、“创建”)

@DisplayNameFor(model=>model.Contatti.Nome) @DisplayNameFor(model=>model.Contatti.Citta) @DisplayNameFor(model=>model.Contatti.ContattoID) @DisplayNameFor(model=>model.Contatti.CodicePostale) @DisplayNameFor(model=>model.Contatti.Email) @DisplayNameFor(model=>model.Contact.Address) @DisplayNameFor(model=>model.Contact.CompanyId) @DisplayNameFor(model=>model.Contact.ContactId) @DisplayNameFor(model=>model.Company.CompanyName) @foreach(模型中的var项目) { 对于(int i=0;i<@item.Contatti.Count();i++)//此处出现第二个错误 { @item.Contatti[i].Nome @item.Contatti[i].Citta @item.Contatti[i].ContattoID @item.Contatti[i].代码邮递 @item.Contatti[i].电子邮件 @item.Contatti[i].地址 @item.Contatti[i].CompanyId @item.Contatti[i].联系人ID @item.Contatti[i].公司名称 @ActionLink(“编辑”,“编辑”,新的{id=item.ContattoID})| @ActionLink(“详细信息”,“详细信息”,新的{id=item.ContattoID})| @ActionLink(“删除”,“删除”,新的{id=item.ContattoID}) } }
控制器获取方法中的第一个错误:

错误CS1929“ContactsUni2”不包含“AsEnumerable”的定义,并且最佳扩展方法重载“DataTableExtensions.AsEnumerable(DataTable)”需要“DataTable”类型的接收器

当我尝试这样做时,它说:

无法从方法组转换为“contactsuni2”

视图循环中的第二个错误:

错误CS1061“Contatti”不包含“Count”的定义,并且找不到接受“Contatti”类型的第一个参数的扩展方法“Count”(是否缺少using指令或程序集引用?)

只要用这个:

contactlist.Add(CU); 
方法
List.Add()
需要单个项,而CU是单个项

要解决您视图中的问题,只需在顶部添加一行
@using System.Linq
,编译器就会知道在哪里查找


有关可计数的其他信息:

如果您有项的集合或序列,但该集合与诸如
foreach(集合中的var项)之类的构造不兼容,则只需要
AsEnumerable()

在这种情况下,您可以尝试使用
foreach(collection.AsEnumerable()中的var项)


在某些情况下,.NET Framework可能已经提供了
.AsEnumerable()
的实现,在其他情况下,您可能需要自己提供。但在您的问题中,您甚至根本不需要它。

您有两个不同的问题:

  • 在您的
    ContactsUni21Controller
    中,您正在对一个未实现
    IEnumerable
    的对象调用
    AsEnumerable
    ;您正在尝试将
    ContactsUni2
    的实例添加到
    列表中,这里不需要强制转换,因为它是列表所包含的同一类型的实例

  • 正如@CodeCaster所提到的,您没有将
    @using System.Linq;
    语句放在Razor视图中,从而阻止您访问
    IEnumerable.Count
    方法


  • 您在代码中有这些名称空间,但在您的
    contactlist.Add(CU);