c# - How to query for all EntityA that contain any EntityB within a collection of EntityBs? -
given following 2 example c# entity classes ...
class entitya { public int entityaid { get; set; } public virtaul icollection<entityb> bs { get; set; } } class entityb { public int entitybid { get; set; } public string foobar { get; set; } public virtual entitya { get; set; } }
how go using entity framework + linq query entitya
contain entityb
within provided collection of entityb
?
to clarify, if have following subset of entityb
...
var bs = new list<entityb> { new entityb { foobar = "a" }, new entityb { foobar = "b" }, new entityb { foobar = "c" } };
i want find entitya
entitya.bs
collection contains of possible entityb
within above bs
collection.
so when asked thought meant had dbset of entityb. see you're trying find entitya entityb defined in given set, can simplify this:
var bs = collection of entityb var dbset = dbset of type entitya var entities = dbset.where(m => m.bs.any(b => bs.any(b2 => b2.foobar == b.foobar).tolist();
so through collection of entitya , return entitya has entityb foobar equal of in given collection.
i query entitya since you're getting collection of entityas. if wanted entityb following.
var entities = entitybset.where(m => bs.any(b => b.foobar == m.foobar)) .select(m => m.a) .tolist();
Comments
Post a Comment