【问题标题】:Find index of number if item exists in ObservableCollection in C#/WPF如果项目存在于 C#/WPF 的 ObservableCollection 中,则查找数字的索引
【发布时间】:2018-06-17 11:35:06
【问题描述】:

在下面的代码中,我正在检查 ObservableCollection 中是否存在某个项目,如果存在,我想获取它的索引,以便我可以将该项目移动到顶部,但我无法弄清楚。

我的对象:

public class RecentFile
{
    public string FileName { get; set; }
    public string DateAdded { get; set; }
}

查找项目索引的代码:

if (recentFilesObservableCollection.Any(f => f.FileName == "MyFileName")) {

    foreach (RecentFile file in recentFilesObservableCollection) {
        if (file.FileName == "MyFileName") {
            var x = RecentFilesDataGrid[(recentFilesObservableCollection.IndexOf(file)];
        }
    }
}

如果项目存在,获取索引号的最佳方法是什么?

最终我需要做的是……

  1. 检查项目是否存在
  2. 如果确实将其移至列表顶部

【问题讨论】:

标签: c#


【解决方案1】:

检查项目是否存在。如果它确实让项目找到它的索引。从那里移动它。

//Check if item exists
if (recentFilesObservableCollection.Any(f => f.FileName == "MyFileName")) {
    //If it does, get item
    var file = recentFilesObservableCollection.First(f => f.FileName == "MyFileName");
    //grab its index
    var index = recentFilesObservableCollection.IndexOf(file);
    if (index > 0)
        //move it to the top of the list
        recentFilesObservableCollection.Move(index, 0);
}

用更少的枚举实现相同结果的另一种替代方法。

var item = recentFilesObservableCollection
    .Select((file, index) => new { file, index })
    .FirstOrDefault(f => f.file.FileName == "MyFileName"));
if(item != null && item.index > 0) // or if(item?.index > 0)
    recentFilesObservableCollection.Move(item.index, 0);

【讨论】:

  • @ Nkosi - 它成功了,谢谢。快速提问,有没有办法在检查文件是否存在时忽略大小写?现在,如果我检查 "MyFileName""myFileName" 是否存在,它会返回 false。
  • @fs_tigre 使用string.Equals(f.file.FileName, "MyFileName", StringComparison.InvariantCultureIgnoreCase) 比较它们忽略大小写。
  • 嗯,我对f.file.FileName 有点困惑我不确定file 是什么,我收到一条错误消息,说它在当前上下文中不存在。
  • @fs_tigre 我虽然你指的是第二个例子。只需将其替换为第一个示例中使用的内容
  • 哦,抱歉,我使用的是您第一个示例中的代码。
【解决方案2】:

您可以创建类似于List<T>.FindIndex(...) 方法的扩展方法:

public static class ObservableCollectionExtensions
{
    public static int FindIndex<T>(this ObservableCollection<T> ts, Predicate<T> match)
    {
        return ts.FindIndex(0, ts.Count, match);
    }

    public static int FindIndex<T>(this ObservableCollection<T> ts, int startIndex, Predicate<T> match)
    {
        return ts.FindIndex(startIndex, ts.Count, match);
    }

    public static int FindIndex<T>(this ObservableCollection<T> ts, int startIndex, int count, Predicate<T> match)
    {
        if (startIndex < 0) startIndex = 0;
        if (count > ts.Count) count = ts.Count;

        for (int i = startIndex; i < count; i++)
        {
            if (match(ts[i])) return i;
        }

        return -1;
    }
}

用法:

int index = recentFilesObservableCollection.FindIndex(f => f.FileName == "MyFileName");
if (index > 0)
{
    // Move it to the top of the list
    recentFilesObservableCollection.Move(index, 0);
}

【讨论】:

    【解决方案3】:

    试试这个: Int index=YourCollection.IndexOf(x => x.fileName == “名称”); 然后使用 MoveItem 获取当前索引和目标索引。

    【讨论】:

    • 据我所知,没有可用的内置FindIndex 方法。我猜你的意思是IndexOf
    • 我做了你也可以在msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx方法下找到它
    • 这段代码无法编译,ObservableCollection&lt;T&gt;.IndexOf(...) 需要 T 类型的参数作为输入,而不是 lambda 表达式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多