Mat*_*sen 8 c# linq lambda coding-style
我决定第一次试用LINQ尝试解决这个问题.
我第一次涉足LINQ精彩世界的结果如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>()
{
"fred-064528-NEEDED1",
"xxxx",
"frederic-84728957-NEEDED2",
"sam-028-NEEDED3",
"-----",
"another-test"
};
var result =
from s in list
where (from c in s where c == '-' select c).Count() == 2
select s.Substring(s.LastIndexOf("-") + 1);
foreach (string s in result)
Console.WriteLine(s);
Console.WriteLine("Press Enter");
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何改进上述解决这个人为设计的小例子的方法.我对是否使用了最好的验证方法,或者我如何本地化"按Enter"或类似的东西不太感兴趣; 我只是想使用这个例子来学习更多关于LINQ的知识.
var result =
from s in list
where s.Count(x => x == '=') == 2
select s.Substring(s.LastIndexOf("-") + 1);
Run Code Online (Sandbox Code Playgroud)