Viks
Viks

Reputation: 413

Using the All operator

I would like to see an example that makes the best use of the ALL operator when using a parent child reltaionship in LINQ. Can you show me one please?

Upvotes: 1

Views: 175

Answers (4)

Amy B
Amy B

Reputation: 110111

IEnumerable<CD> goodCDs = CDs
  .Where(cd => cd.Songs.All(song => song.Rating > 6))

Upvotes: 0

pondermatic
pondermatic

Reputation: 6583

Many LINQ examples here: http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062770

The All() extension method checks a predicate against all the items; for example, at execution:

if(order.Lines.All(l=>l.IsClosed)) order.Close();

(checks all lines are closed, and if so, closes the order)

of in a query:

var qry = from order in ctx.Orders
         where order.CustomerId = id
         select new {
            order.OrderId,
            IsShipped = order.Lines.All(l => l.IsShipped)
         };

Upvotes: 1

eglasius
eglasius

Reputation: 36037

If you want to get the parents along with whether all its childrens are active.

from p in MyContext.Parents
select new
{
   p,
   ChildrensActive = p.Childrens.All(c=> c.IsActive)
}

Upvotes: 0

Related Questions