开发者

Error in lambda: No implicit convertion between

I convert one list of to list and get error: "Type of conditional expression cannot be determined because there is no implicit conversion between System.Collections.Generic.List and 'voi开发者_JAVA技巧d'

return (topics.Select(c => new TopicUi()
{
    Bookmarks = new List<Bookmark>().Add(new Bookmark { Id = c.BookmarkId, Name = c.BookmarkName })
})).ToList();

Why?


The Add method of List has a return type of void, this should work for you:

return (topics.Select(c => new TopicUi
{
    Bookmarks = new List<Bookmark> {
      new Bookmark { Id = c.BookmarkId, Name = c.BookmarkName }
    }
})).ToList();


At the very leat, fix this line

Bookmarks = new List<Bookmark>().Add(new Bookmark { Id = c.BookmarkId, Name = c.BookmarkName })

Add is a void returning method. The line should be

Bookmarks = new List<Bookmark> { new Bookmark { Id = c.BookmarkId, Name = c.BookmarkName } }

In order to properly use collection initialization.


Rather than calling the Add method of List<T>, you can just use the object initialization syntax:

return (topics.Select(c => new TopicUi()
{
    Bookmarks = new List<Bookmark>() 
                  { new Bookmark { Id = c.BookmarkId, Name = c.BookmarkName } }
                )
})).ToList();


IList.Add has no return type. Try this;

Func<YourCType, IList<Bookmark>> createBookmarkListFunc = (c) => 
{ 
    var list = new List<Bookmark>() { new Bookmark { Id = c.BookmarkId, Name = c.BookmarkName };
    return list;
});
return (topics.Select(c => new TopicUi()
{
    Bookmarks = createListFunc(c)
})).ToList();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜