netmajor netmajor - 1 year ago 110
C# Question

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(
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?

Answer Source

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

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


IQueryable<object> q = ...;
List<object> l = new List<object>(q);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download