How can I convert 'System.Collection.Generic.List<String>' into 'System.Windows.Documents.List'?

966 Views Asked by At

I have a table of doctors in my database. So I'm trying to get the list of the firstName of the doctors in my database. In the ViewModel class I'm using this code to get it

public List  DoctorsList ()
{
    // string mainconn = Configuration
    List ListOfDoctors;

    using (var context = new GlabDbContext())
    {
        var result = (from c in context.Doctors
                              select c.LastName).ToList();

        ListOfDoctors = result;
    }

    return ListOfDoctors;
}

I want to use this function like a method of my ViewModel class an it will have a return. But I'm getting an error saying that:

Impossible to convert implicitely 'System.Collections.Generic.List into 'System.Windows.Documents.List'?

I try to cast the result like this

public List  DoctorsList ()
{
    // string mainconn = Configuration
    List ListOfDoctors;

    using (var context = new GlabDbContext())
    {
        var result = (from c in context.Doctors
                              select c.LastName).ToList();
                              
        **ListOfDoctors = (list)result;**
    }

    return ListOfDoctors;
}

but I get an error at the run time for the app.

How can I resolve the problem?

3

There are 3 best solutions below

0
Hans Kesting On BEST ANSWER

Your List ListOfDoctors appears to be really an

System.Windows.Documents.List ListOfDoctors;

and this is also the return type of your method.

Your var result really is

System.Collections.Generic.List<string> result

The two types are not compatible, meaning that you cannot cast one to the other (as the error message says).

I suspect you don't really want to return a Documents.List but a List<string> (containing just those names). So:

  • Remove a using System.Windows.Documents; from your file
  • Change all List to List<string>
0
BlueFox On

You can try this like this:

System.Windows.Documents.List listx = new System.Windows.Documents.List();

foreach (var r in result)
{
    listx.ListItems.Add(new ListItem(new Paragraph(new Run(r)));
}
1
Arsen Khachaturyan On

Most probably you had an intention to use the IList or List<T>, but accidentally imported the System.Windows.Documents.List and all the later errors appeared because of that.

Please, take a moment and think what return type do you really need and if you want to return a collection of string elements then either use List<string> or IList as a return type:

public IList DoctorsList() // or return List<string> or IList<string> (generic version)
{
    // string mainconn = Configuration
    IList ListOfDoctors;

    using (var context = new GlabDbContext())
    {
        var result = (from c in context.Doctors
            select c.LastName).ToList();

        ListOfDoctors = result;
    }

    return ListOfDoctors;
}

Shorter version (c# 8 compatable):

public IList DoctorsList() // or return List<string> or IList<string> (generic version)
{
    using var context = new GlabDbContext();
    
    return (from c in context.Doctors select c.LastName).ToList();
}

Remember:

You can do casting only with compatible types, which means that there should be either appropriate conversion operators defined for both types or a Base⟶Derived class relationship should exist between types.


For more information:

  • About Casting and type conversions read here.
  • About Type conversion operators read here.