我有以下查询在LinqPad中成功运行:
var results =
from container in Container
join containerType in ContainerType on container.ContainerType equals containerType
where containerType.ContainerTypeID == 2
select new { ContainerID = container.ContainerID, TypeID = container.ContainerTypeID};
results.Dump();
我想将选择更改为使用索引器,以便选择看起来像这样:
select new { ContainerID = container.ContainerID, TypeID = container.ContainerTypeID, ContainerIndex = index };
我似乎无法正确理解的是选择使用选择索引器的正确语法。
谢谢你的帮助。
最佳答案
您无法使用查询表达式格式获取索引,但是可以用点表示法使用overload for Select
来实现。您可以坚持查询表达式格式的大部分内容,然后在额外的选择投影中添加索引:
var tmp =
from container in Container
join containerType in ContainerType
on container.ContainerType equals containerType
where containerType.ContainerTypeID == 2
select new { ContainerID = container.ContainerID,
TypeID = container.ContainerTypeID};
var results = tmp.Select((x, index) => new { x.ContainerID, x.TypeID,
ContainerIndex = index });