Cannot implicitly convert type 'string' to 'System.Collections.Generic.List<string>'

72,934

Solution 1

This is the problematic line:

List<string> listString = listObject.OrderBy(x => x.m_Type).ToString();

ToString returns a string, and you're trying to assign to a List<string> variable.

You need to do something like this:

List<string> listString = listObject.OrderBy(x => x.m_Type)
                                    .Select(x => x.ToString())
                                    .ToList();

This statement will order your listObject enumerable to the order that your want, then convert the values to strings, and finally convert the enumerable to a List<string>.

Solution 2

Look at this code:

List<string> listString = listObject.OrderBy(x => x.m_Type).ToString();

The right hand side expression is calling ToString() on the result of OrderBy - which isn't useful itself, and will result in a string.

You're then trying to assign that string expression to a new variable of type List<string>. That's not going to work.

I suspect you just want ToList instead of ToString:

List<string> listString = listObject.OrderBy(x => x.m_Type).ToList();

Solution 3

I think you want ToList instead of ToString.

Also, if all you're trying to do is remove duplicates, you can simply use LINQ and do this:

List<string> listString = listObject.OrderBy(x => x.m_Type).
    Select(x => x.ToString()).Distinct().ToList();

Solution 4

There was still a bit of correcting to be done. Here's the complete code:

private void PopulateStringDropdownList(List<ObjectInfo> listObjects, object selectedType = null)

        {
            List<string> listString = listObjects.OrderBy(x => x.m_Type)
                                             .Select(x => x.m_Type.ToString())
                                             //.Distinct()
                                             .ToList();
            ViewBag.cardType = new SelectList(listString );
        }

That way the correct value found is the string of the Type and not the type of the object.

Thanks everyone for your help!

Solution 5

Convert list of int to list of string:

List<string> listString =  listObject.OrderBy(x => x.m_Type).
Select(x =>x.ToString()).ToList();
Share:
72,934
hsim
Author by

hsim

I am a C# .NET programmer-analyst based in Quebec, Canada.

Updated on July 09, 2022

Comments

  • hsim
    hsim almost 2 years

    This question has probably been answered hundreds of times, but here goes.

    I have this chunck of code:

    private void PopulateStringDropdownList(List<ObjectInfo> listObject,
                                            object selectedType = null)
    {
        List<string> listString = listObject.OrderBy(x => x.m_Type).ToString();
        for (int i = 0; i < listString .Count; i++)
        {
            for (int j = i + 1; j < listString .Count; j++)
            { 
                if (String.Equals(listString [i], listString [j]))
                {
                    listString.RemoveAt(j);
                }
            }
        }
        ViewBag.property1 = new SelectList(listString );
    }
    

    So basically I'm trying to populate a dropdown List with strings coming from a property of each objects contained in the list I pass in parameter.

    But the code doesn't compile because of the error you're seeing up there, and I've yet to understand why exactly. Any help anyone?

  • hsim
    hsim over 11 years
    It's not that I don't want to; rather, it doesn't work. I thought first that it was because the list that receives the data was a string list, but... nope.
  • hsim
    hsim over 11 years
    Oh! Will try that one too.
  • Jon Skeet
    Jon Skeet over 11 years
    @HervéSimard: What do you mean? What failed when you tried this? What's the type of the m_Type field? (Please be more specific in future.)
  • AndyClaw
    AndyClaw over 11 years
    listObject is a list of ObjectInfo, not string, so use listObject.OrderBy(x => x.m_Type).Select(l => l.ToString()).Distinct().ToList()
  • Matthew Strawbridge
    Matthew Strawbridge over 11 years
    @AndyClaw Thanks Andy. Think I was editing my answer just as you commented.