I’ve having trouble figuring out how to select based on a list in a many to many relationship.
I’ve created with entities-framework the following entities and many to many relationship (please correct me if I’m going about this wrong) :
public class Foo {
public int FooID { get; set; }
public string FooName { get; set; }
public virtual ICollection<FooBar> Foo_Bar { get; set; }
}
public class Bar {
public int BarID { get; set; }
public string BarName { get; set; }
public virtual ICollection<FooBar> Foo_Bar { get; set; }
}
public class FooBar {
public int FooBarID{ get; set; }
public virtual int BarID { get; set; }
public virtual int FooID { get; set; }
}
In my code my controller will receive a list of Foo and I need to find all the Bar with those foo ( both with only and with any )
I’m at a loss for where to start really… this is all I’ve come up with:
public PartialViewResult SearchAnyBar(List<Foo> foos) {
List<FooBar> foobars = _db.FooBar.Select(fb => fb).ToList<FooBar>();
List<Bar> searchedBars = new List<Bar>();
foreach (Foo f in foos)
{
foreach (FooBar fXb in foobars)
{
if (fXb.FooID == f.FooID)
{
searchedBars.Add(_db.Bar.Where(b => b.BarID == fXb.BarID).FirstOrDefault());
}
}
}
return PartialView("The View", searchBars);
}
This works for the grabbing any Bar however:
-
I’m pretty positive there’s a much better way of doing this, is there a way to select based on a list instead of going about 2 foreach loop?
-
I’m not sure how to go about getting a list of Foos where the Foo has ALL the Bars and not just ANY.
Remove FooBar class.
Just create a
public virtual ICollection<Foo> Foos {get;set;}in you Bar classand a
public virtual ICollection<Bar> Bars {get;set;}in your Foo ClassThis will create a many to many relationship (with a relation table named [Foo-Bar] or something like that in your db… but who minds, you will be using objects).
then
any query :
not sure I understood well the “only” and “any”, but if you have problems with the other query… ask.