4

私は次のものを持っています:

     foreach (string applicationName in applicationNames)
     {
     _uow.Applications.Add(
        new Application 
        { 
            Name = applicationName, 
            ModifiedDate = DateTime.Now,
            TestAccounts = (from  testAccountName in testAccountNames
                            select new TestAccount
                            { 
                               Name = testAccountName , 
                               ModifiedDate = DateTime.Now 
                           })
        });
     }

これに伴う問題は、選択時にVS2012IDEでエラーが発生することです。ここにそれは言います:

Error   3   Cannot implicitly convert type 
'System.Collections.Generic.IEnumerable<Relational.Models.TestAccount>' to
'System.Collections.Generic.ICollection<Relational.Models.TestAccount>'.
An explicit conversion exists (are you missing a cast?

アプリケーションクラスは次のとおりです。

public partial class Application
{
    public Application()
    {
        this.TestAccounts = new List<TestAccount>();
    }

    public int ApplicationId { get; set; }
    public string Name { get; set; }
    public virtual byte[] Version { get; set; }
    public System.DateTime ModifiedDate { get; set; }
    public virtual ICollection<TestAccount> TestAccounts { get; set; }
}
4

2 に答える 2

5

ToListを使用する:

 foreach (string applicationName in applicationNames)
 {
 _uow.Applications.Add(
    new Application 
    { 
        Name = applicationName, 
        ModifiedDate = DateTime.Now,
        TestAccounts = (from  testAccountName in testAccountNames
                        select new TestAccount
                        { 
                           Name = testAccountName , 
                           ModifiedDate = DateTime.Now 
                       }).ToList()
    });
 }
于 2013-03-25T20:24:07.310 に答える
1

にキャストする必要がありますIList<TestAccount>

    { 
        Name = applicationName, 
        ModifiedDate = DateTime.Now,
        TestAccounts = (from  testAccountName in testAccountNames
                        select new TestAccount
                        { 
                           Name = testAccountName , 
                           ModifiedDate = DateTime.Now 
                       }).ToList()     // <-- try this
    });
于 2013-03-25T20:24:41.590 に答える