gpt4 book ai didi

c# - 在 linq orderby 中过滤值

转载 作者:行者123 更新时间:2023-12-02 05:17:21 24 4
gpt4 key购买 nike

有没有办法在下面的查询中过滤值 -1(来自 IndexOf when txt not found in item)?

var sortedArray = array.OrderBy (item => item.IndexOf(txt)).ToArray();

任何指示都会有所帮助。

如果我使用 Where,它将使用 IndexOf 两次,有没有办法只使用一次 IndexOf

最佳答案

为了避免必须执行相同的 IndexOf 两次,您可以这样做:

var sortedArray = (from a in array
let i = a.IndexOf("txt")
where i >= 0
orderby i
select a).ToArray();

关于c# - 在 linq orderby 中过滤值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14446068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com