皮条客我的LINQ:根据另一篇文章一个学习 [英] Pimp my LINQ: a learning exercise based upon another post
本文介绍了皮条客我的LINQ:根据另一篇文章一个学习的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我决定尝试LINQ首次尝试解决这个问题。
我第一次尝试的结果到LINQ的奇妙世界是这样的:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
命名空间ConsoleApplication2
{
类节目
{
静态无效的主要(字串[] args)
{
名单,LT;字符串>名单=新名单,LT;串>()
{
弗雷德-064528-NEEDED1,
XXXX,
弗雷德里克-84728957-NEEDED2,
SAM-028-NEEDED3,
-----,
的另一个测试
};从s
变种结果=
名单中的
,其中(从C s中,其中c ==' - '选择C).Count之间的()== 2
选择s.Substring(s.LastIndexOf( - )+1);
的foreach(字符串s的结果)
Console.WriteLine(S);
Console.WriteLine(按回车键);
到Console.ReadLine();
}
}
}
我想知道我怎么能改善上述解决这个人为的小例子。我不是太感兴趣,无论是我用过的最好的验证方法,不然我怎么会本地化按回车键之类的东西;我用这个例子来了解一些关于LINQ只是感兴趣。
解决方案
VAR的结果=从s
名单中的
,其中s.Count(X => X =='=')== 2
选择s.Substring(s.LastIndexOf( - )+ 1);
I decided to try out LINQ for the first time to try and solve this question.
The results of my first foray into the wonderful world of LINQ looked like this:
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();
}
}
}
I'd like to know how I can improve the above solution to this contrived little example. I'm not too interested in whether I've used the best validation method, or how I could localise "Press Enter" or anything like that; I'm just interested in using this example to learn a little more about LINQ.
解决方案
var result =
from s in list
where s.Count(x => x == '=') == 2
select s.Substring(s.LastIndexOf("-") + 1);
这篇关于皮条客我的LINQ:根据另一篇文章一个学习的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文