views:

70

answers:

4
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.

I cant seem to get it to work, can someone help me?

+1  A: 

You can use the Count field to check if the collection is empty or not

so you will end up with something like this :

if(devices.Count > 0)
{
   //foreach loop
}
Manaf Abu.Rous
Yeah I've actually tried that but it gives me an error: Error 1 'string' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
Prd
A: 

How about checking array length

If (arr2.length > 0) {
                    foreach (int i in arr2)
                    {
                        Device d = Device.Find(i);
                        d.Projects.Add(project);
                        d.SaveAndFlush();
                    }
}
Roadie57
gives me the same error :/
Prd
then var devices is not returning the type you expect.
Roadie57
If people were not using `var` most of the would be a lot happier…
Ondrej Tucny
+1  A: 

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 a string. Do you mean collection is the checkbox values? What is the exact type of collection?

Any object that implements ICollection or ICollection<T> can be checked whether it's empty or not by checking if the Count property is greater than zero.

thecoop
its a formcollection. I am converting it to a int array so the values can be processed. Ive updated the code to show the entire action as it is now. This code works as long as you select at least 1 checkbox which is the problem.
Prd
formcollection only returns controls that contain data. that is normal behaviour
Roadie57
+1  A: 

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.

using System;
using System.Collections.Generic;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> emptyList = new List<string>();

            foreach (string item in emptyList)
            {
                Console.WriteLine("This will not be printed");
            }

            List<string> list = new List<string>();

            list.Add("item 1");
            list.Add("item 2");

            foreach (string item in list)
            {
                Console.WriteLine(item);
            }

            Console.ReadLine();
        }
    }
}
DarkIce
Yes i'm sorry im an idiot. Misread the error i got when trying a if statement. It wasn't being caused by the forearch trying to loop through an empty collection. putting the conversion to int array within the if statement solves it.
Prd