简体   繁体   English

不包含“添加”的定义

[英]Does not contain a definition for “Add”

I'm trying to make the same thing like in this thread, but I'm getting error: 我试图像在这个线程中做同样的事情,但我得到错误:

'System.Collections.Generic.IEnumerable' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?) 'System.Collections.Generic.IEnumerable'不包含'Add'的定义,也没有扩展方法'Add'接受类型为'System.Collections.Generic.IEnumerable'的第一个参数'(你是否缺少using指令)或汇编参考?)

Here is my code: 这是我的代码:

[HttpPost]
public ActionResult Create(ANIME anime)
{
    var db = new MainDatabaseEntities();
    var newanime = new ANIME
    {
        ID_AN = anime.ID_AN,
        TITLE_OR = anime.TITLE_OR,
        TITLE_EN = anime.TITLE_EN,
        GENRES = new List<GENRES>()
    };

    foreach (var selectedAnime in anime.GENRES.Where(c => c.isSelected))
    {
        var genre = new GENRES { ID_GE = selectedAnime.ID_GE };
        db.GENRES.Attach(genre);
        newanime.GENRES.Add(genre); <--- this is the error line
    }

    db.ANIME.Add(newanime);
    db.SaveChanges();
    return RedirectToAction("Index");
}

ANIME: ANIME:

public partial class ANIME
{
    public int ID_AN { get; set; }
    public string TITLE_OR { get; set; }
    public string TITLE_EN { get; set; }

    public virtual IEnumerable<GENRES> GENRES { get; set; }
}

GENRES: 流派:

public partial class GENRES
{
    public int ID_GE { get; set; }
    public string GENRE { get; set; }
    public bool isSelected { get; set; }
    public virtual ICollection<ANIME> ANIME { get; set; }
}

The error is in the line newanime.GENRES.Add(genre) in HttpPost . 错误在HttpPost中的newanime.GENRES.Add(genre)中。 I added using System.Linq to all models and controllers but it doesn't help. using System.Linq添加到所有模型和控制器,但它没有帮助。 Any ideas how to resolve this? 任何想法如何解决这个问题?

EDIT: 编辑:

After repairing this a new error arrived. 修复后,出现了新的错误。 I think it's not related to above one but I don't want to spam unnecessary threads. 我认为这与上面的内容无关,但我不想发送不必要的线程。

Error message: 错误信息:

The entity or complex type 'MainDatabaseModel.GENRES' cannot be constructed in a LINQ to Entities query. 无法在LINQ to Entities查询中构造实体或复杂类型“MainDatabaseModel.GENRES”。

Related code: 相关代码:

public ActionResult Create()
{
    var db = new MainDatabaseEntities();
    var viewModel = new ANIME
    {
        GENRES = db.GENRES.Select(c => new GENRES
        {
            ID_GE = c.ID_GE,
            GENRE = c.GENRE,
            isSelected = false
        }).ToList()
    };
    return View(viewModel);       
}

You have a IEnumerable property that you're initialising with a List. 您有一个IEnumerable属性,您正在使用List进行初始化。 The List class implements the IEnumerable interface. List类实现IEnumerable接口。

When you are invoking something like this: 当你调用这样的东西时:

IEnumerable myList = new List<MyType>();

you're saying that you want for your object the features of the IEnumerable interface that are also inherited in the List class. 你说你想为你的对象提供IEnumerable接口的特性,这些特性也在List类中继承。 In this case, the method Add isn't part of the IEnumerable interface, because it's a method of the List class only, and you have that exception. 在这种情况下,方法Add不是IEnumerable接口的一部分,因为它只是List类的一个方法,并且您有该异常。

You have then to change the type of your property, from IEnumerable<YourType> to IList<YourType> (more info about IList here ). 然后,您必须更改属性的类型,从IEnumerable<YourType>IList<YourType> (有关IList的更多信息,请IList<YourType> 此处 )。 In this way, the exception about Add method won't be thrown. 这样,就不会抛出有关Add方法的异常。

Try this: 尝试这个:

public partial class ANIME
{

    public int ID_AN { get; set; }
    public string TITLE_OR { get; set; }
    public string TITLE_EN { get; set; }

    public virtual ICollection<GENRES> GENRES { get; set; } // Use ICollection here
}

The answer for your second question is that you cannot (and should not be able to) project onto a mapped entity. 第二个问题的答案是你不能(也不应该)投射到映射的实体上。 You can, however, project onto an annonymous type or on data transfer object. 但是,您可以投影到匿名类型或数据传输对象。

See this thread: The entity cannot be constructed in a LINQ to Entities query 请参阅此线程: 无法在LINQ to Entities查询中构造实体

Also, please do not extend your initial question with a totally new, unrealted one in the future. 此外,请不要在未来使用全新的,未实现的问题扩展您的初始问题。 It makes it hard to follow... 它很难跟上......

IEnumerable<> is just a sequence of items. IEnumerable<>只是一系列项目。 You can't add items to it and you can't remove items from it. 您无法向其中添加项目,也无法从中删除项目。 You can query it. 你可以查询它。

If you need to add items, you will need a collection implementing at least the ICollection<> interface or IList<> interface. 如果需要添加项目,则需要至少实现ICollection<>接口或IList<>接口的集合。

Good news is that you can use IEnumerable<> there like as follows 好消息是你可以使用IEnumerable<> ,如下所示

var list = new List<GENRES>();
var newanime = new ANIME
{
    ID_AN = anime.ID_AN,
    TITLE_OR = anime.TITLE_OR,
    TITLE_EN = anime.TITLE_EN,
    GENRES = list
};

list.Add(genre);

But this has limited possibilities. 但这种可能性有限。 You won't be able to add new items once you leave the scope and loose a reference to the local list variable. 离开作用域并松开对本地list变量的引用后,您将无法添加新项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM