views:

95

answers:

4

I have 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' to 'System.Collections.Generic.IList'. An explicit conversion exists (are you missing a cast?)

Any idea how solve my problem ? :/

+3  A: 

Try this -->

 new DzieckoAndOpiekun(
                         p.Imie,
                         p.Nazwisko,
                         p.Opiekun.Imie,
                         p.Opiekun.Nazwisko).ToList()
Ryk
+2  A: 

You can use the .ToList() method to convert the IQueryable result returned to an IList, as shown below, after the linq query.

   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).ToList()
                  ;
        result.AddRange(resultV);
    }
    return result;
}
fletcher
i have then error :Error 1 'WcfService1.DzieckoAndOpiekun' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'WcfService1.DzieckoAndOpiekun' could be found (are you missing a using directive or an assembly reference?)
netmajor
+1  A: 

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);
MainMa
Ideal solution, Tnx!! :)
netmajor
+5  A: 

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

fuwaneko
This is a far better answer than the others. Especially if `AddRange()` accepts an `IEnumerable<T>` (which it almost certainly would...)
Dean Harding