[英]Finding comma separated String values in a List<int>
我有一個逗號分隔的字符串變量,我需要檢查其值是否存在於給定列表中
string freeServices = "1,7,13,21";
List<int> selectedServices = booking.SelectedServices.Select(x => x.ServiceID).ToList();
我已經嘗試過這樣的事情
if (selectedServices.Contains(Convert.Int32(freeServices.Split(','))
{
}
我可以這樣做嗎? 還是有其他簡便的方法可以找到所選ID列表中的免費服務ID?
要檢查,所有值都包含在SelectedServices
:
string freeServices = "1,7,13,21";
var values = freeServices.Split(',').Select(o=>Convert.ToInt32(o)).ToList();
List<int> selectedServices = booking.SelectedServices.Select(x => x.ServiceID).ToList();
if (selectedServices.All(o=>values.Contains(o))
{
}
試試下面的查詢,您將包含記錄
var containsValues = booking.SelectedServices.where(e=> freeServices.Split(',').Contains(e.ServiceID));
您可以使用All
和int.Parse
;
string freeServices = "1,7,13,21";
List<int> selectedServices = booking.SelectedServices.Select(x => x.ServiceID).ToList();
var splittedFreeServices = freeServices.Split(',').Select(k => int.Parse(k));
var result = selectedServices.All(x => splittedFreeServices.Contains(x));
if (result) //booking.SelectedServices contains all elements of freeServices as integer
{
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.