Link to home
Start Free TrialLog in
Avatar of dionisio_79
dionisio_79

asked on

Cannot convert from Linq.IQueryable to System.Collections.Generic.List

Hi Experts,
I have the following code:

ZefiroContext zefiroContext = new ZefiroContext();
            var r = from p in zefiroContext.Articoli
                    where p.Id == IdArticolo
                    select p.Fornitori.ToList();
            List<Fornitore> result = r;
            return result;

The line         List<Fornitore> result = r;

 gives me the following exception:

Error      1      Cannot implicitly convert type 'System.Linq.IQueryable<System.Collections.Generic.List<prova2.Model.Fornitore>>' to 'System.Collections.Generic.List<prova2.Model.Fornitore>'. An explicit conversion exists (are you missing a cast?)
Avatar of p_davis
p_davis

List<Fornitore> result = r.ToList();
Avatar of dionisio_79

ASKER

I tryed with

List<Fornitore> result = r.ToList()

and I got the following error message:

Error      1      Cannot implicitly convert type 'System.Collections.Generic.List<System.Collections.Generic.List<prova2.Model.Fornitore>>' to 'System.Collections.Generic.List<prova2.Model.Fornitore>'
Maybe this?


List<Fornitore> result = r.Cast<prova2.Model.Fornitore>.ToList()
I tryed with
List<Fornitore> result = r.Cast<prova2.Model.Fornitore>.ToList()

and I got the following error message:

Error      1      'System.Linq.ParallelEnumerable.Cast<TResult>(System.Linq.ParallelQuery)' is a 'method', which is not valid in the given context
sorry... i freehanded that one

List<Fornitore> result = r.Cast<prova2.Model.Fornitore>().ToList()
Now I got the follwing runtime exception (I try to translate it from italian):

LINQ to Entities doesn't recognize the method  'System.Collections.Generic.List`1[prova2.Model.Fornitore] ToList[Fornitore](System.Collections.Generic.IEnumerable`1[prova2.Model.Fornitore])', which therefore can't be converted in an archive expression
didn't realize you were using entities

maybe try it with out the tolist

List<Fornitore> result = r.Cast<prova2.Model.Fornitore>();
Error      1      Cannot implicitly convert type 'System.Linq.IQueryable<prova2.Model.Fornitore>' to 'System.Collections.Generic.List<prova2.Model.Fornitore>'. An explicit conversion exists (are you missing a cast?)
ASKER CERTIFIED SOLUTION
Avatar of dionisio_79
dionisio_79

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I was trying to select multiple collections and that was the problem