Reputation: 99
I'm using Entity Framework. I have the following entities:
public class Articolo
{
..
public virtual ICollection<Fornitore> Fornitori { get; set; }
}
public class Fornitore
{
...
public virtual ICollection<Articoli> Articoli { get; set; } }
the following code:
List<Fornitore> result = new List<Fornitore>();
var r = zefiroContext.Articoli.Where(p => p.Id == IdArticolo).Select(p => p.Fornitori).ToList();
result = r;
gives a compiler error:
Cannot implicitly convert type 'System.Collections.Generic.List < System.Collections.Generic.ICollection < prova2.Model.Fornitore>>' to 'System.Collections.Generic.List < prova2.Model.Fornitore>'
how can I get my List < Fornitore >
?
Upvotes: 1
Views: 2493
Reputation: 26766
Each Articolo has Many Fornitori - So you're getting a list of lists...
You may be meaning to get only the matching single item with a matchign id...
var r = zefiroContext.Articoli.First(p => p.Id == IdArticolo).Select(p => p.Fornitori).ToList();
Upvotes: 0
Reputation: 82375
Well you are actually trying to select multiple collections and I think that is where your problem is, changing your Select
to SelectMany
may resolve the issue for you but I am not sure if that is your intended functionality. What is going to happen is the SelectMany
is going to condense all the individual result sets into a single collection which then can be turned into a list of the model type vs a list of collections of the model type.
var r = zefiroContext.Articoli.Where(p => p.Id == IdArticolo).SelectMany(p => p.Fornitori).ToList();
Upvotes: 4