35

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?

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
netmajor
  • 6,507
  • 14
  • 68
  • 100
  • Related posts : [Convert an IQueryable linq query to IEnumerable cancels out linq optimized way to work?](https://stackoverflow.com/q/14842176/465053) & [Should I always return IEnumerable instead of IList?](https://stackoverflow.com/q/1072614/465053) – RBT Jul 20 '21 at 03:23

7 Answers7

54

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);
Arseni Mourzenko
  • 50,338
  • 35
  • 112
  • 199
  • Of course `.ToArray()` Linq extension could also be used in this case (because a `T[]` is an `IList` as well). But what does the `AddRange` method actually require? Maybe he could just use `var` (which would mean `IQueryable` here), and `AddRange` would be happy with that? Note: `IQueryable<>` inherits `IEnumerable<>`. – Jeppe Stig Nielsen Aug 29 '16 at 10:27
11

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

fuwaneko
  • 1,155
  • 8
  • 10
6

If using a where clause be sure to include .First() if you do not want a IQueryable object.

kravits88
  • 12,431
  • 1
  • 51
  • 53
4

Try this -->

 new DzieckoAndOpiekun(
                         p.Imie,
                         p.Nazwisko,
                         p.Opiekun.Imie,
                         p.Opiekun.Nazwisko).ToList()
Ryk
  • 3,072
  • 5
  • 27
  • 32
  • .ToList() will work properly what if i am using .Take(2) instead. It will throw same error – manny Dec 19 '16 at 09:16
2

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
  • 13,380
  • 9
  • 52
  • 69
  • 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 Aug 04 '10 at 05:57
1

I've got solved my problem this way in EF on list of records from db

ListOfObjectToBeOrder.OrderBy(x => x.columnName).ToList();
X-Coder
  • 2,632
  • 2
  • 19
  • 17
1

SonarQube reported Return an empty collection instead of null. and I had a problem with the error with casting as in the title of this question. I was able to get rid of both only using return XYZ.ToList().AsQueryable(); in a method with IQueryable like so:

public IQueryable<SomeType> MethodName (...) {
  IQueryable<SomeType> XYZ;
  ...
  return XYZ.ToList().AsQueryable();
}

Hope so it helps for those in a similar scenario(s).

Daniel Danielecki
  • 8,508
  • 6
  • 68
  • 94
  • 1
    This is a different case. Almost 100% the reverse. Also, this looks more like bad design. Returning `IQueryable` from a method usually has the purpose of enabling a user to compose on the result. A `List` disguised as an `IQueryable` looks more like a hack than deliberate design. – Gert Arnold Oct 08 '20 at 09:14
  • @GertArnold thanks for the quick feedback, really appreciated! – Daniel Danielecki Oct 08 '20 at 09:24