下面是一个示例,演示如何使用
let
关键词:
var results =
(from type in Assembly.GetExecutingAssembly().GetTypes()
// Try to find first such interface and assign the result to 'ifc'
// Note: we use 'FirstOrDefault', so if it is not found, 'ifc' will be null
let ifc = type.GetInterfaces().FirstOrDefault(x =>
x.IsGenericType &&
x.GetGenericTypeDefinition() == typeof(MyInterface<,>))
// Filtering and projection can now use 'ifc' that we already have
where ifc != null
// Similarly to avoid multiple calls to 'GetGenericArguments'
let args = ifc.GetGenericArguments()
select new ResultObj(type, args[0], args[1])).ToList();
这个
让
关键字的工作方式有点像变量声明,但它位于LINQ查询中—它允许您创建一个变量,该变量存储查询中稍后在多个位置需要的某些结果。您也提到了“join”,但它主要用于类似数据库的join(我不确定它在这里如何应用)。