如何确定字符串是否包含字符串列表的任何匹配项
你好,说我有一个字符串列表:
var listOfStrings = new List<string>{"Cars", "Trucks", "Boats"};
我有一个带有名称字段的车辆选项。
我想找到名称与 listOfStrings 中的项目之一匹配的车辆。
我正在尝试使用 linq 来完成此操作,但目前似乎无法完成。
var matchingVehicles = Vehicles.Where(v => v.Name == one of the listOfStringItem)
有人能帮我解决这个问题吗?
Hi say I have a list of strings:
var listOfStrings = new List<string>{"Cars", "Trucks", "Boats"};
and I have a vehicles options which has a Name field.
I want to find the vehicles where the name matches one of the items in the listOfStrings.
I'm trying to do this with linq but can't seem to finish it at the moment.
var matchingVehicles = Vehicles.Where(v => v.Name == one of the listOfStringItem)
Can anybody help me with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用
HashSet
而不是List
,这样您就可以查找字符串而无需循环遍历列表。现在您可以使用
Contains
方法有效地查找匹配项:Use a
HashSet
instead of aList
, that way you can look for a string without having to loop through the list.Now you can use the
Contains
method to efficiently look for a match:这会起作用吗:
would this work:
您可以执行内部联接:
You can perform an Inner Join:
要检查字符串是否包含以下字符之一(布尔输出):
To check if a string contains one of these characters (Boolean output):