Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.IList'

I have a method:

public DzieckoAndOpiekunCollection GetChildAndOpiekunByFirstnameLastname(string firstname, string lastname)
{
    DataTransfer.ChargeInSchoolEntities db = new DataTransfer.ChargeInSchoolEntities();
    DzieckoAndOpiekunCollection result = new DzieckoAndOpiekunCollection();
    if (firstname == null && lastname != null)
    {
        IList<DzieckoAndOpiekun> resultV = from p in db.Dziecko
                      where lastname == p.Nazwisko
                      **select** new DzieckoAndOpiekun(
                     p.Imie,
                     p.Nazwisko,
                     p.Opiekun.Imie,
                     p.Opiekun.Nazwisko)
                  ;
        result.AddRange(resultV);
    }
    return result;
}

and error in selected place :

Error 1 Cannot implicitly convert type 'System.Linq.IQueryable<WcfService1.DzieckoAndOpiekun>' to 'System.Collections.Generic.IList<WcfService1.DzieckoAndOpiekun>'. An explicit conversion exists (are you missing a cast?)

Any idea how solve my problem?

like image 276
netmajor Avatar asked Aug 04 '10 05:08

netmajor


2 Answers

To convert IQuerable or IEnumerable to a list, you can do one of the following:

IQueryable<object> q = ...;
List<object> l = q.ToList();

or:

IQueryable<object> q = ...;
List<object> l = new List<object>(q);
like image 176
Arseni Mourzenko Avatar answered Nov 09 '22 23:11

Arseni Mourzenko


You can replace IList<DzieckoAndOpiekun> resultV with var resultV.

like image 11
fuwaneko Avatar answered Nov 09 '22 22:11

fuwaneko