简体   繁体   中英

cannot convert from 'System.Collections.Generic.List<NS.TagCollection>' to 'NS.TagCollection'

I want to bind a collection consisting of these two objects to a treeview.

class TagMeta
{
    public string cdata { get; set; }
    public string objectId { get; set; }
    public string completed { get; set; }
    public string highlightColor { get; set; }
    public string fontColor { get; set; }

    public int symbol { get; set; }

    public DateTime modified { get; set; }
}

class TagCollection
{
    private List<TagMeta> _tags;

    public string tagName { get; set; }

    public List<TagMeta> tags
    {
        set { _tags = new List<TagMeta>(value); }
        get { return _tags; }
    }
}

Using this view model, consisting of a three table join using Lightspeed as an ORM.

class TagViewModel 
{
    private readonly App _app = ((App)System.Windows.Application.Current);

    private List<TagCollection> _tagCollection = new List<TagCollection>();

    public TagViewModel()
    {
        using (var dbSession = _app.OrmContext.CreateUnitOfWork())
        {
            // get unique tag names
            var collection = 
                from dbTagTypes in dbSession.TagTypes.
                        Distinct().                 
                        OrderBy(o => o.Name).
                        ToList()                         
                select new TagCollection { 
                    tagName = dbTagTypes.Name,
                    // embed the tag meta list
                    tags = (
                        from t in dbSession.Tags
                        join to in dbSession.TagOutlines on t.Outline equals to
                        join tt in dbSession.TagTypes on t.TagType equals tt 
                        where t.TagType.Name.Contains(dbTagTypes.Name)
                        orderby to.CData
                        select new TagMeta {
                            cdata = to.CData, 
                            objectId = to.ObjectId,
                            completed = t.Completed,
                            modified = (DateTime) to.Modified,
                            symbol = tt.Symbol, 
                            highlightColor = tt.HighlightColor,
                            fontColor = tt.FontColor         
                        }
                    ).ToList(),
                };

            // error here    
            _tagCollection.Add(collection.ToList());

        }
    }

    public List<TagCollection> TagCollection
    {
        get { return _tagCollection; }
    }


}

The error message is quite clear, but what is less clear is how I get or convert the LINQ result to the correct type.

I've tried changing var collection = to List<TagCollection> = and also making TagCollection implement IEnumerable

Clearly I'm missing something, but I seem to be going around in circles. What am I doing wrong here please?

Try using the AddRange method which takes an IEnumerable<T> as parameter (which is what you are passing):

_tagCollection.AddRange(collection.ToList());

The Add method expects a single element.

You are trying to add a list of objects when the Add method expects a single item.

Use AddRange :

_tagCollection.AddRange(collection.ToList());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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