none
Unable to cast object of type 'System.Collections.Generic.List`1[System.String]' to type 'System.IConvertible'.

    Question

  • Hi,

    I had post this questions on my previous post but I think it is better to star a new post. Thanks.

    I am having another problem with LINQ and lambda. Please could someone shed some light?

    This is the error I am getting:

    Unable to cast object of type 'System.Collections.Generic.List`1[System.String]' to type 'System.IConvertible'.

    var allAuditCGPerm = (from p in ctx.tblPermissions
                                              where p.PermissionCategoryId == 4
                                              select p).ToList();

    //Get selected items of ListBox which will return a list of strings
    var lstSelectedItems = lstAuditCG.SelectedValues();

    // Error happens here:
    var assignedPermissions =
                        allAuditCGPerm.All(p => p.PermissionId == Convert.ToInt32(lstSelectedItems.ToList()));

    public static List<string> SelectedValues(this ListBox lbox)
    {
                var selectedValues = new List<string>();

                int[] selectedIndeces = lbox.GetSelectedIndices();
                return selectedIndeces.Select(i => lbox.Items[i].Value).ToList();

          
    }

    Cheers

    C

    Tuesday, November 15, 2011 1:43 PM

Answers

  • Yes, You can use the following code using Where() condition

    class Program

        {

            static void Main(string[] args)

            {

                IList<TheEntity> entireList = new List<TheEntity> 

                {

                    new TheEntity() { PermissionId=1 },

                    new TheEntity() { PermissionId=2 },

                    new TheEntity() { PermissionId=3 },

                    new TheEntity() { PermissionId=4 }

                };

     

                IList<int> selectedIdList = new List<int> { 1, 2 };

     

                var assignedPermissions = entireList.Where(e => selectedIdList.Contains(e.PermissionId)).ToList(); 

     

     

            }

        }

     

        class TheEntity

        {

            public int PermissionId;

        }


    Resolving n Evolving in C#
    Tuesday, November 15, 2011 2:34 PM

All replies

  • Hello Claudio,

     

    The All() method is used to check whether all the items satisfy the condition.  It is not to select the items.

    I believe the condition you needed is whther PermissionId is in the selectedItems list.

    You can modify the code as below:

    var assignedPermissions =
                        allAuditCGPerm.All(p => lstSelectedItems.ToList().IndexOf(p.PermissionId.ToString()) > 0);

     

    If your intention was different please let us know.

     

    Regards,

    Jean Paul


    Resolving n Evolving in C#
    Tuesday, November 15, 2011 1:54 PM
  • Hi Jean,

    I tried your code but the assignedPermissions is being set to false.

    Since I selected one item in the ListBox, the id from listbox and object should have matched and assignedPermissions should contain at least one object.

    It seems that your code is not looping the collections. Any ideas?

    var assignedPermissions =
                         allAuditCGPerm.All(p => lstSelectedItems.ToList().IndexOf(p.PermissionId.ToString()) > 0); 

    Cheers

    C

     

     

    Tuesday, November 15, 2011 2:14 PM
  • I think I need something like this but using LINQ and lambda

    foreach(var perm in  allAuditCGPerm)
    {
           foreach(var assignedPerm in lstSelectedItems)
           {
                if(perm.PermissionId == int.Parse(assignedPerm))
                   // do something
           }

    }

    Cheers

    C

    Tuesday, November 15, 2011 2:16 PM
  • The All() method returns only bool (true/false)

    It checks whether all items in the allAuditCGPerm satisfies particular condition.

     

    For gaining a grip on it: Try using a simple list of 5 numbers


    Resolving n Evolving in C#
    Tuesday, November 15, 2011 2:19 PM
  • On 11/15/2011 8:43 AM, Claudio Pallone wrote:
     
    > // Error happens here:
    > *var assignedPermissions =*
    > *allAuditCGPerm.All(p => p.PermissionId ==
    > Convert.ToInt32(lstSelectedItems.ToList()));*
     
     
    You can't use the "Convert" statement in the situation to select anything.
     
    You need to come-up with a statement that's not going to Covert anything
    on the fly in the selection process.
     
    Tuesday, November 15, 2011 2:19 PM
  • ok, could you please shed some light on how to select all the items that satisfy the condition that both ids match and add them to the new variable assignedPermissions ?

    Cheers

    C

     

    Tuesday, November 15, 2011 2:22 PM
  • This should point you in the right direction.

    http://blog.ysatech.com/post/2009/10/21/Linq-convert-double-to-string.aspx

    Tuesday, November 15, 2011 2:29 PM
  • Yes, You can use the following code using Where() condition

    class Program

        {

            static void Main(string[] args)

            {

                IList<TheEntity> entireList = new List<TheEntity> 

                {

                    new TheEntity() { PermissionId=1 },

                    new TheEntity() { PermissionId=2 },

                    new TheEntity() { PermissionId=3 },

                    new TheEntity() { PermissionId=4 }

                };

     

                IList<int> selectedIdList = new List<int> { 1, 2 };

     

                var assignedPermissions = entireList.Where(e => selectedIdList.Contains(e.PermissionId)).ToList(); 

     

     

            }

        }

     

        class TheEntity

        {

            public int PermissionId;

        }


    Resolving n Evolving in C#
    Tuesday, November 15, 2011 2:34 PM
  • Hi Jean,

    Thanks very much for your source code and help.

    I have tried to apply your sample code to my problem but I think my collections are not matching and your code is not working.

    My lstSelectedItems is a List<string>. For some reason the Contains() method is not working. I have tried using the AsEnumerable() method before contains but to no avail.

    var assignedPermissions = allAuditCGPerm.Where(p => lstSelectedItems.AsEnumerable().Contains(p.PermissionId)).ToList();

    So what I need is to select all the objects in allAuditCGPerm that has a permissionId matching to the string permission ids in the lstSelectedItems ListBox collection.

    P.S.

    This is the method I use to  create the lstSelectedItems

    public static List<string> SelectedValues(this ListBox lbox)
    {
                var selectedValues = new List<string>();

                int[] selectedIndeces = lbox.GetSelectedIndices();
                return selectedIndeces.Select(i => lbox.Items[i].Value).ToList();
          
    }

    Cheers

    C

     

    Tuesday, November 15, 2011 2:55 PM
  • Adding ToString() seems to have solved the problem. What do you think? Is this right?

    var assignedPermissions = allAuditCGPerm.Where(p => lstSelectedItems.AsEnumerable().Contains(p.PermissionId.ToString())).ToList();

    Cheers

     

    Tuesday, November 15, 2011 3:00 PM
  • True.  It is right..

    As your list is containing string, your comparison should also be in string.

     

    Good Job Claudio.. I believe you enjoyed the adventure :)


    Resolving n Evolving in C#
    Tuesday, November 15, 2011 6:16 PM
  • Hi Jean,

    You are right. What an adventure :-)

    I am having problems with another LINQ query and was wondering if you could have a look at it.

    I get it to work using a foreach loop but I would like to use LINQ instead.

    This is the link

    Using LINQ to add object to collection

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ac504087-45f9-494f-b2b9-724abdb1f272

    Cheers

    C

    Tuesday, November 15, 2011 7:40 PM