LINQ 相当于以下代码?

发布于 2024-11-09 02:37:36 字数 448 浏览 0 评论 0原文

我正在学习 LINQ,希望以下方法能提供一些帮助。如何重写以下方法来使用 LINQ?

private bool IsInList(string file, List<FileInfo> excelList)
{
    if (excelList != null && excelList.Count > 0)
    {
        foreach (FileInfo f in excelList)
        {
            if (string.Compare(f.FullName, file, StringComparison.OrdinalIgnoreCase) == 0)
            {
                return true;
            }
        }
    }

    return false;
}

I'm in the process of learning LINQ and would like some help the following method. How can I rewrite the following method to use LINQ?

private bool IsInList(string file, List<FileInfo> excelList)
{
    if (excelList != null && excelList.Count > 0)
    {
        foreach (FileInfo f in excelList)
        {
            if (string.Compare(f.FullName, file, StringComparison.OrdinalIgnoreCase) == 0)
            {
                return true;
            }
        }
    }

    return false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

海夕 2024-11-16 02:37:36

我将更改第二个参数以接受 IEnumerable ,这样您就不会仅限于列表。

private bool IsInList(string file, IEnumerable<FileInfo> excelList)
{
    return excelList != null && excelList.Any(f => f.FullName.Equals(file, StringComparison.OrdinalIgnoreCase));
}

I'd change the second parameter to accept an IEnumerable<FileInfo> instead so you're not limiting yourself to just lists.

private bool IsInList(string file, IEnumerable<FileInfo> excelList)
{
    return excelList != null && excelList.Any(f => f.FullName.Equals(file, StringComparison.OrdinalIgnoreCase));
}
棒棒糖 2024-11-16 02:37:36

使用 lambda:

private bool IsInList(string file, List<FileInfo> excelList)
{
    return excelList != null && excelList.Any(f => string.Compare(f.FullName, file, StringComparison.OrdinalIgnoreCase) == 0);
}

Using lambdas:

private bool IsInList(string file, List<FileInfo> excelList)
{
    return excelList != null && excelList.Any(f => string.Compare(f.FullName, file, StringComparison.OrdinalIgnoreCase) == 0);
}
去了角落 2024-11-16 02:37:36

像这样的东西:

private bool IsInList(string file, List<FileInfo> excelList)
{
    if (excelList == null) return false;
    return excelList.Any(f => string.Compare(f.FullName, file, StringComparison.OrdinalIgnoreCase) == 0));
}

Something like this:

private bool IsInList(string file, List<FileInfo> excelList)
{
    if (excelList == null) return false;
    return excelList.Any(f => string.Compare(f.FullName, file, StringComparison.OrdinalIgnoreCase) == 0));
}
末が日狂欢 2024-11-16 02:37:36

尝试一下——完全未经测试。

private bool IsInList(string file, List<FileInfo> excelList)
{
    if (excelList != null && excelList.Count > 0)
    {
        return excelList.Any(f => string.Compare(f.FullName, file, StringComparison.OrdinalIgnoreCase) == 0);
    }

    return false;
}

Give this a try -- completely untested.

private bool IsInList(string file, List<FileInfo> excelList)
{
    if (excelList != null && excelList.Count > 0)
    {
        return excelList.Any(f => string.Compare(f.FullName, file, StringComparison.OrdinalIgnoreCase) == 0);
    }

    return false;
}
好多鱼好多余 2024-11-16 02:37:36

这应该做...

if (excelList != null && excelList.Count > 0)
{
    return (from f in excelList
            where (string.Compare(f.FullName, file, StringComparison.OrdinalIgnoreCase) == 0)
            select f).Any();
}
return false;

This should do...

if (excelList != null && excelList.Count > 0)
{
    return (from f in excelList
            where (string.Compare(f.FullName, file, StringComparison.OrdinalIgnoreCase) == 0)
            select f).Any();
}
return false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文