是否有可能使用查询语言编写此...而不是方法链?
notifications.Where((n, index) => n.EventId == m_lastSelectedEventID)
.Select((n, index) => new {Position = index}).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
谢谢,拉杜
Jon*_*eet 40
不,查询表达式语法不支持那些我担心的重载.
另一方面,如果在开始时使用Select过载一次创建具有索引和值的匿名类型,则可以在查询表达式中使用该对序列.例如:
var query = from pair in sequence.Select((value, index) => new { value, index })
where pair.index % 3 == 0
select string.Format("{0}: {1}", pair.index, pair.value);
Run Code Online (Sandbox Code Playgroud)
编辑:请注意,在您的示例代码中,您始终先过滤,然后获取结果序列中第一个条目的索引.该索引将始终为0.如果您想实际找到所选ID 的原始索引notifications,我怀疑您真的想要:
int? index = notifications.Select((n, index) => new { n, index })
.Where(pair => pair.n.EventId == m_lastSelectedEventID)
.Select(pair => (int?) pair.index)
.FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
(这将返回Nullable<int>的null,如果没有找到.)
| 归档时间: |
|
| 查看次数: |
13311 次 |
| 最近记录: |