var userIds = books.Select( i => new{i.CreatedById,i.ModifiedById}).Distinct();
var lst = from i in userIds select i.CreatedById;
var lst1 = from i in userIds select i.ModifiedById;
var lstfinal = lst.Concat(lst1).Distinct();
任何其他方式获得相同的结果???
这里:books 是书籍对象的集合,即 IEnumerable。
谢谢
最佳答案
替代解决方案 - SelectMany
来自所需属性的数组:
var lstfinal = books.SelectMany(b => new [] { b.CreatedById, b.ModifiedById })
.Distinct();
或枚举书籍收藏两次。
Union
排除重复项,因此您无需调用 Distinct
(我会走这条路):var lstfinal = books.Select(b => b.CreatedById)
.Union(books.Select(b => b.ModifiedById));
关于LINQ:由同一类型对象集合的多个属性组成的单个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15113740/