我有一个像这样的参数列表:
public class parameter
{
public string name {get; set;}
public string paramtype {get; set;}
public string source {get; set;}
}
IEnumerable<Parameter> parameters;
还有一个字符串数组我想要与之进行比较。
string[] myStrings = new string[] { "one", "two"};
我希望能够迭代参数列表并检查源属性是否等于myStrings数组中的任何一个。我可以使用嵌套的foreach来完成此操作,但是我想学习如何以更好的方式完成,因为我一直在玩弄Linq,并且喜欢可枚举的扩展方法(如where等),因此嵌套的foreach感觉不太对。是否有更优雅的Linq/Lambda/Delegate方法来做到这一点呢?谢谢。
parameters.Any(x => myStrings.Contains(x.source));
不是比你的第一个示例更好吗? - FluppeIntersectBy()
的扩展方法,它消除了首先对源进行投影的必要性-https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.intersectby - Rand Random