LINQ查询中的匿名函数错误
这是我尝试实现的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查询表达式不支持该语法。
您应该直接调用
OrderBy
:Query expressions don't support that syntax.
You should call
OrderBy
directly:为了解决您的“有更好的方法”问题,这样吧:由于分数显然始终是一位数字(可能前面有
-
),我们可以使用正则表达式来提取分数,用let
定义一个范围变量,然后按它排序:产生
正向后向断言
(?<=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 withlet
, then order by it:produces
The positive look-behind assertion
(?<=Score=\")
says to only consider matching if the previous text isScore="
; only the optional-
and the digit are actually captured into theMatch
, though.