The application must provide a component part list for any product. The component part list must give the quantity of
each distinct part that is required to manufacture that product.
You need to create a LINQ expression that delivers a a result of type IEnumerable<Tuple<int,Part>> to meet the requirements.
Which expression should you use?
A.
IEnumerable<Tuple<int, Part>> result = part.Children
.Distinct()
.GroupBy(p => p)
.Select(g => Tuple.Create(g.Count(), g.Key));
B.
IEnumerable<Tuple<int, Part>> result = part.Descendants
.GroupBy(p => p)
.Select(g => Tuple.Create(g.Count(), g.Key));
C.
IEnumerable<Tuple<int, Part>> result = part.Descendants
.ToDictionary(c => c)
.Select(d => Tuple.Create(d.Value.Children.Count(), d.Key));
D.
IEnumerable<Tuple<int, Part>> result = part.Children
.GroupBy(p => p)
.Select(g => Tuple.Create(g.Count(), g.Key));
E.
IEnumerable<Tuple<int, Part>> result = part.Descendants
.Distinct()
.GroupBy(p => p)
.Select(g => Tuple.Create(g.Count(), g.Key));
What is the difference between C and D? How could candidate know that parts are stored under “Descendants” collection and not under “Children”?
Descendants is not a collection. It’s an XElement class dynamic property: http://msdn.microsoft.com/en-us/library/vstudio/bb943867(v=vs.100).aspx
No such class with a name ‘Children’ exists.
sorry, i mean between B and D
This one is part of a case study, that information is given there.
Why doesn’t suggested answer use Distinct() method? Like E?
Because Distinct would remove duplicate parts, and the question asks you to get their totals.