检查集合是否为空
public ActionResult Create(FormCollection collection, FormCollection formValue)
{
try
{
Project project = new Project();
TryUpdateModel(project, _updateableFields);
var devices = collection["devices"];
string[] arr1 = ((string)devices).Split(',');
int[] arr2 = Array.ConvertAll(arr1, s => int.Parse(s));
project.User = SessionVariables.AuthenticatedUser;
var time = formValue["Date"];
project.Date = time;
project.SaveAndFlush();
foreach (int i in arr2)
{
Device d = Device.Find(i);
d.Projects.Add(project);
d.SaveAndFlush();
}
return RedirectToAction("Index");
}
catch (Exception e)
{
return View(e);
}
}
我想将 foreach 包装在 if 语句中,检查是否
var devices = collection["devices"];
为空。如果其为空,则不应执行 foreach。根据记录,collection["devices"] 是表单中复选框值的集合。
public ActionResult Create(FormCollection collection, FormCollection formValue)
{
try
{
Project project = new Project();
TryUpdateModel(project, _updateableFields);
var devices = collection["devices"];
string[] arr1 = ((string)devices).Split(',');
int[] arr2 = Array.ConvertAll(arr1, s => int.Parse(s));
project.User = SessionVariables.AuthenticatedUser;
var time = formValue["Date"];
project.Date = time;
project.SaveAndFlush();
foreach (int i in arr2)
{
Device d = Device.Find(i);
d.Projects.Add(project);
d.SaveAndFlush();
}
return RedirectToAction("Index");
}
catch (Exception e)
{
return View(e);
}
}
I want to wrap the foreach in a if statement which checks if
var devices = collection["devices"];
is empty or not. If its empty the for each should not be executed. For the record, collection["devices"] is a collection of checkbox values from a form.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用
Count
字段来检查集合是否为空,这样您最终会得到如下结果:
You can use the
Count
field to check if the collection is empty or notso you will end up with something like this :
您可以使用方法
Any
来了解集合是否为任意元素。You can use the method
Any
to know if a collection as any element.您不需要检查集合是否为空,如果为空,则 ForEach 内的代码将不会执行,请参阅下面的示例。
You do not need to check if the collection is empty, if it is empty the code inside the ForEach will not be executed, see my example below.
按照目前的情况,您的代码将无法工作,正如您所说的
collection["devices"]
是复选框值的集合,但您却将其转换为string< /代码>。您的意思是
collection
是复选框值吗?集合
的确切类型是什么?任何实现了
ICollection
或ICollection
的对象都可以通过检查Count
属性是否大于零来检查它是否为空。Your code, as it stands, won't work, as you say that
collection["devices"]
is a collection of checkbox values, and yet you're casting it to astring
. Do you meancollection
is the checkbox values? What is the exact type ofcollection
?Any object that implements
ICollection
orICollection<T>
can be checked whether it's empty or not by checking if theCount
property is greater than zero.如何检查数组长度
How about checking the array length
这在 Dot Net Core 中对我有用,但仅适用于模型的 IEnumerable,而不是实体
(我从 AutoMapper 得到了一些帮助)
将其转换为列表,然后检查容量
This worked for me in Dot Net Core but only for IEnumerable of Models not Entities
(I got a bit of help from AutoMapper)
Cast it as a List then check the Capacity