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();
精彩评论