考虑以下代码片段:
有没有什么方法可以神奇地将一个
考虑以下代码片段:
public static class MatchCollectionExtensions
{
public static IEnumerable<T> AsEnumerable<T>(this MatchCollection mc)
{
return new T[mc.Count];
}
}
并且这个类:
public class Ingredient
{
public String Name { get; set; }
}
有没有什么方法可以神奇地将一个
MatchCollection
对象转换为一个Ingredient
集合?使用情况可能如下所示:var matches = new Regex("([a-z])+,?").Matches("tomato,potato,carrot");
var ingredients = matches.AsEnumerable<Ingredient>();
更新
一个基于纯LINQ的解决方案也可以。