如何在 LINQ 中执行此查询?

发布于 2024-11-04 13:30:25 字数 574 浏览 2 评论 0原文

考虑这个简短的片段:

var candidateWords = GetScrabblePossibilities(letters);
var possibleWords = new List<String>();

foreach (var word in candidateWords)
{
    if (word.Length == pattern.Length)
    {
        bool goodMatch = true;
        for (int i=0; i < pattern.Length && goodMatch; i++)
        {
            var c = pattern[i];
            if (c!='_' && word[i]!=c)
                goodMatch = false;
        }
        if (goodMatch)
            possibleWords.Add(word);
    }
}

有没有办法使用 LINQ 清楚地表达这一点?
它是什么?

Consider this brief snippet:

var candidateWords = GetScrabblePossibilities(letters);
var possibleWords = new List<String>();

foreach (var word in candidateWords)
{
    if (word.Length == pattern.Length)
    {
        bool goodMatch = true;
        for (int i=0; i < pattern.Length && goodMatch; i++)
        {
            var c = pattern[i];
            if (c!='_' && word[i]!=c)
                goodMatch = false;
        }
        if (goodMatch)
            possibleWords.Add(word);
    }
}

Is there a way to express this cleanly using LINQ?
What is it?

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

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

发布评论

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

评论(2

默嘫て 2024-11-11 13:30:25

一个简单的翻译是使用 < 将每个候选词覆盖在模式词上code>Zip 运算符。

var possibleWords = from word in candidateWords
                    where word.Length == pattern.Length
                       && word.Zip(pattern, (wc, pc) => pc == '_' || wc == pc)
                              .All(match => match)
                    select word;

如果您确实想专注于索引,可以使用 Range 运算符:

var possibleWords = from word in candidateWords
                    where word.Length == pattern.Length
                       && Enumerable.Range(0, word.Length)
                                    .All(i => pattern[i] == '_' 
                                           || pattern[i] == word[i])
                    select word;

编辑:

正如 David Neale 指出的,Zip 运算符在 .NET 4.0 之前不可用。实现它微不足道然而你自己。

A straightforward translation would be to overlay each candidate-word over the pattern-word using the Zip operator.

var possibleWords = from word in candidateWords
                    where word.Length == pattern.Length
                       && word.Zip(pattern, (wc, pc) => pc == '_' || wc == pc)
                              .All(match => match)
                    select word;

If you really want to focus on the indices, you can use the Range operator:

var possibleWords = from word in candidateWords
                    where word.Length == pattern.Length
                       && Enumerable.Range(0, word.Length)
                                    .All(i => pattern[i] == '_' 
                                           || pattern[i] == word[i])
                    select word;

EDIT:

As David Neale points out, the Zip operator is not available before .NET 4.0. It's trivial to implement it yourself, however.

迷荒 2024-11-11 13:30:25

另一种不使用 Zip 的方法:

var possibleWords = candidateWords.Where(w => w.Length == pattern.Length && 
                                         word.Select((c, i) => pattern[i] == '_' || 
                                                               pattern[i] == c)
                                             .All(b => b))
                                  .ToList();

Another way of doing this w/o Zip:

var possibleWords = candidateWords.Where(w => w.Length == pattern.Length && 
                                         word.Select((c, i) => pattern[i] == '_' || 
                                                               pattern[i] == c)
                                             .All(b => b))
                                  .ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文