我对Linq很新。我想将这些代码行转换为linq lambda表达式,如果有意义我该如何实现?
foreach (var Type in Essay.Text)
{
string text =
$"{"here is result"}\n{method(Type)}";
if (text.Length <= 20 && !string.IsNullOrEmpty(method(Type)))
{
Essay.Total += text;
}
}
答案 0 :(得分:2)
借助Resharper:
Essay.Total = string.Concat(
Essay.Text.Select(Type => new {Type, text = $"{"here is result"}\n{method(Type)}"})
.Where(@t => @t.text.Length <= 20 && !string.IsNullOrEmpty(method(@t.Type)))
.Select(@t => @t.text)
);
答案 1 :(得分:2)
这样的事情:
foreach (var type in from type in Essay.Text
let text = $"{"here is result"}\n{method(Type)}"
where text.Length <= 20 && !string.IsNullOrEmpty(method(Type)) select type)
{
Essay.Total += type;
}
答案 2 :(得分:0)
一些指示:
这应该适合你:
var texts = from type in essay.Text
let methodResult = method(type)
where !string.IsNullOrEmpty(methodResult)
let text = $"here is result\n{methodResult}"
where text.Length <= 20
select text;
essay.Total += string.Concat(texts);