Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在列表中查找逗号分隔的字符串值_C#_C# 4.0 - Fatal编程技术网

C# 在列表中查找逗号分隔的字符串值

C# 在列表中查找逗号分隔的字符串值,c#,c#-4.0,C#,C# 4.0,我有一个逗号分隔的字符串变量,我需要检查它的值是否存在于给定的列表中 string freeServices = "1,7,13,21"; List<int> selectedServices = booking.SelectedServices.Select(x => x.ServiceID).ToList(); 我能做这个吗?或者有没有其他简单的方法来确定所选id列表中的免费服务id?要检查,所有值都包含在所选服务中: 尝试下面的查询,您将获得包含的记录 var cont

我有一个逗号分隔的字符串变量,我需要检查它的值是否存在于给定的列表中

string freeServices = "1,7,13,21";
List<int> selectedServices = booking.SelectedServices.Select(x => x.ServiceID).ToList();

我能做这个吗?或者有没有其他简单的方法来确定所选id列表中的免费服务id?

要检查,所有值都包含在所选服务中:


尝试下面的查询,您将获得包含的记录

var containsValues = booking.SelectedServices.where(e=> freeServices.Split(',').Contains(e.ServiceID));
您可以使用All和int.Parse


一个简单的内循环可能看起来效率很低,但你知道,所有那些花哨的Linq东西在内部都是这样做的。似乎人们忘记了,有时候,你只需要自己编写东西,而不是依靠花哨的技巧。你将为所选服务中的每一项执行freeServices.Split'
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));
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
{

}