It's pretty straight forward:
var list = ids.Select(id => new Book { Id = id }).ToList();
Or if you prefer query syntax:
var list = (from id in ids select new Book { Id = id }).ToList();
Also note that the ToList()
is only necessary if you really need List<Book>
. Otherwise, it's generally better to take advantage of Linq's lazy evaluation abilities, and allow the Book
objects objects to only be created on demand.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…