LINQ查询中的匿名函数错误

发布于 2024-12-02 17:58:22 字数 653 浏览 0 评论 0原文

这是我尝试实现的代码:

var query = from line in thedata
            orderby () =>  {
                int a = line.IndexOf("Score=\"");
                if (line[a + "Score=\"".Length + 1] == '-') {
                    return
                        int.Parse(line[a + "Score=\"".Length + 1].ToString()
                      + line[a + "Score=\"".Length + 2]);
                }

                return int.Parse(line[a + "Score=\"".Length + 1].ToString());
            }
            select line;

其中 thedata 是字符串列表。

编译器在 orderby 上显示错误(无法推断类型参数)。我该如何重写这个函数来解决这个错误?顺便说一句,在这种情况下有没有更好的方法获得负数?

Here is the code that I am trying implement:

var query = from line in thedata
            orderby () =>  {
                int a = line.IndexOf("Score=\"");
                if (line[a + "Score=\"".Length + 1] == '-') {
                    return
                        int.Parse(line[a + "Score=\"".Length + 1].ToString()
                      + line[a + "Score=\"".Length + 2]);
                }

                return int.Parse(line[a + "Score=\"".Length + 1].ToString());
            }
            select line;

Where thedata is a list of strings.

The compiler shows an error on the orderby (the type arguments cannot be inferred). How can I rewrite this function to resolve this error? As a side point, is there a better way of getting negative numbers in this type of situation?

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

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

发布评论

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

评论(2

落墨 2024-12-09 17:58:22

查询表达式不支持该语法。

您应该直接调用 OrderBy

var query = data.OrderBy(line => { ... });

Query expressions don't support that syntax.

You should call OrderBy directly:

var query = data.OrderBy(line => { ... });
撞了怀 2024-12-09 17:58:22

为了解决您的“有更好的方法”问题,这样吧:由于分数显然始终是一位数字(可能前面有 -),我们可以使用正则表达式来提取分数,用 let 定义一个范围变量,然后按它排序:

// With a positive look-behind assertion for Score="
// Capture:
//  an optional minus
//  then
//  a digit
Regex score = new Regex("(?<=Score=\")-?\\d");

string[] thedata = new[] { "Score=\"5\"", "Score=\"-2\"" };

var query = from line in thedata
            let scoreAsString = score.Match(line).Value
            orderby int.Parse(scoreAsString)
            select line;

foreach (var outputLine in query)
{
    Console.WriteLine(outputLine);
}

产生

Score="-2"
Score="5"

正向后向断言 (?<=Score=\") 表示仅考虑匹配前面的文字是分数=“;不过,实际上只有可选的 - 和数字被捕获到 Match 中。

To address your "is there a better way" question, how about this: since the score is apparently always a single digit (perhaps preceded by a -), we can use a regex to pull out the score, define a range variable with let, then order by it:

// With a positive look-behind assertion for Score="
// Capture:
//  an optional minus
//  then
//  a digit
Regex score = new Regex("(?<=Score=\")-?\\d");

string[] thedata = new[] { "Score=\"5\"", "Score=\"-2\"" };

var query = from line in thedata
            let scoreAsString = score.Match(line).Value
            orderby int.Parse(scoreAsString)
            select line;

foreach (var outputLine in query)
{
    Console.WriteLine(outputLine);
}

produces

Score="-2"
Score="5"

The positive look-behind assertion (?<=Score=\") says to only consider matching if the previous text is Score="; only the optional - and the digit are actually captured into the Match, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文