【发布时间】:2021-03-18 00:37:03
【问题描述】:
我有以下查询,我在哪里使用带有 select 的 where 子句并获取结果(librarySourceRowInputs 的列表),并且想使用聚合而不是(where 和 select)。
在这个过程中,我试图访问聚合中的相同变量,但没有得到任何引用,下面是相同的代码
public static LibrarySourceTableInput CreateLibrarySourceTableInput<T>(List<T> libraries, string mechanicalLibraryName)
where T : ISourceOfData => new LibrarySourceTableInput()
{
LibrarySourceRowInputs = libraries?
.Where(l => l != null)
.Select(l => new LibrarySourceRowInput()
{
LibrarySourceId = l.Id,
SourceOfDataId = l.SourceOfData.Id
}).ToList() ?? new(),
MappedLibrarySource = mechanicalLibraryName
};
下面是我尝试使用聚合的函数
// trying to replace the where and select in above with aggregate
var LibrarySourceRowInputs = libraries.Aggregate(new List<LibrarySourceRowInput>(),
(prev, next) =>
// here I am not getting any reference for Id
);
我不确定这是实现此目的或任何其他方式的正确方法。任何人都可以就此提出任何想法,非常感谢!
【问题讨论】:
标签: c# linq .net-core lambda linq-to-objects