C# 如何将这个foreach循环转换为linq?

C# 如何将这个foreach循环转换为linq?,c#,linq,lambda,C#,Linq,Lambda,我对林克很陌生。我想将这些代码行转换为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

我对林克很陌生。我想将这些代码行转换为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;
     }
}
foreach(散文文本中的变量类型)
{
字符串文本=
$“{”这里是结果“}\n{method(Type)}”;
如果(text.Length,借助于:

article.Total=string.Concat(
散文.Text.Select(Type=>new{Type,Text=$“{”这里是结果“}\n{method(Type)}”)
.Where(@t=>@t.text.Length@t.text)
);
在以下人员的帮助下:

article.Total=string.Concat(
散文.Text.Select(Type=>new{Type,Text=$“{”这里是结果“}\n{method(Type)}”)
.Where(@t=>@t.text.Length@t.text)
);
在以下人员的帮助下:

article.Total=string.Concat(
散文.Text.Select(Type=>new{Type,Text=$“{”这里是结果“}\n{method(Type)}”)
.Where(@t=>@t.text.Length@t.text)
);
在以下人员的帮助下:

article.Total=string.Concat(
散文.Text.Select(Type=>new{Type,Text=$“{”这里是结果“}\n{method(Type)}”)
.Where(@t=>@t.text.Length@t.text)
);
类似这样的内容:

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;
}
foreach(从文章中键入var.Text
让text=$“{”这里是结果“}\n{method(Type)}”
其中text.Length如下所示:

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;
}
foreach(从文章中键入var.Text
让text=$“{”这里是结果“}\n{method(Type)}”
其中text.Length如下所示:

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;
}
foreach(从文章中键入var.Text
让text=$“{”这里是结果“}\n{method(Type)}”
其中text.Length如下所示:

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;
}
foreach(从文章中键入var.Text
让text=$“{”这里是结果“}\n{method(Type)}”
其中text.Length几个指针:

  • 没有理由调用方法两次,只需使用let缓存它
  • 您还可以在构造最终字符串之前检查文本的长度
这将为您完成以下工作:

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);
var Text=来自文章中的类型。Text
let methodResult=方法(类型)
其中!string.IsNullOrEmpty(methodResult)
让text=$“这是结果\n{methodResult}”
其中text.Length几个指针:

  • 没有理由调用方法两次,只需使用let缓存它
  • 您还可以在构造最终字符串之前检查文本的长度
这将为您完成以下工作:

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);
var Text=来自文章中的类型。Text
let methodResult=方法(类型)
其中!string.IsNullOrEmpty(methodResult)
让text=$“这是结果\n{methodResult}”
其中text.Length几个指针:

  • 没有理由调用方法两次,只需使用let缓存它
  • 您还可以在构造最终字符串之前检查文本的长度
这将为您完成以下工作:

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);
var Text=来自文章中的类型。Text
let methodResult=方法(类型)
其中!string.IsNullOrEmpty(methodResult)
让text=$“这是结果\n{methodResult}”
其中text.Length几个指针:

  • 没有理由调用方法两次,只需使用let缓存它
  • 您还可以在构造最终字符串之前检查文本的长度
这将为您完成以下工作:

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);
var Text=来自文章中的类型。Text
let methodResult=方法(类型)
其中!string.IsNullOrEmpty(methodResult)
让text=$“这是结果\n{methodResult}”


其中text.Length您不应该使用
类型的名称,因为它在C#中已经存在。您的对象应该命名为其他名称。我建议您首先用StringBuilder替换字符串串联
随笔.Total+=text;
。好的,谢谢您的帮助advices@someone嗨,你愿意接受下面的答案吗?你应该请勿使用
类型的名称,因为它已存在于C#中。您的对象应命名为其他名称。首先,您应该用StringBuilder替换字符串串联
随笔。Total+=text;
。好的,谢谢您的帮助advices@someone您好。您愿意接受下面的一个答案吗?您不应该使用
键入
,因为它在C#中已经存在。您的对象应该命名为其他名称。首先,您应该用StringBuilder替换字符串连接
随笔.Total+=text;
。好的,谢谢您的帮助advices@someone您好。您想接受下面的一个答案吗?您不应该使用
类型的名称,因为它已经存在存在于C#中。您的对象应命名为其他名称。我想您应该首先用StringBuilder替换字符串连接
article.Total+=text;
。好的,谢谢您的帮助advices@someone您好。您想接受下面的答案吗?您可以用
字符串.Concat
@KooKiz替换
字符串.Join
,不知道这种重载方法谢谢你的回答,当我想帮助resharper时,它会转换为linq,但不是lambda表达式,有没有办法在resharper设置中将它转换为lambda?你可以用
字符串替换
字符串。Join
替换为
字符串。Concat
@KooKiz真的,不知道这种重载方法谢谢你的回答是,当我想帮助resharper时,它会转换为linq,但不是lambda表达式,有没有办法在resharper设置中将它转换为lambda?你可以替换
字符串。用
字符串加入
。Concat
@KooKiz真的不知道这种重载方法谢谢你的回答,当我想帮助resharp时呃,它转换为linq,但不是lambda表达式,有没有办法在resharper设置中将其转换为lambda?您可以用
string.C替换
string.Join