0

This is the query I have:

MyRepeater.DataSource = Objects
    .GroupBy(p => p.FAC_ID)
    .Select(p => p.First())
    .First().Dipartimento.AreeDipartimento
    .SelectMany(p => p.Aree);

but it says that can't deduce arguments using the SelectMany.

Instead, if I do:

.SelectMany(p => p.Aree.AREA);

it works! But I need a collection/list of Aree (object), not Aree.AREA (string).

Where am I wrong?

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
markzzz
  • 47,390
  • 120
  • 299
  • 507
  • 6
    What's the type of `Aree`? If it's not a sequence it sounds like you want `Select` instead of `SelectMany`. – Lee Sep 10 '15 at 08:14
  • Yes you are right, it seems to works! I don't got the differences between them so... – markzzz Sep 10 '15 at 08:17
  • 1
    What are you trying to achieve at all? You are grouping by `FAC_ID`, then you are just taking the arbitrary first of each group, from these you take an arbitrary object and so on. If you aren't interested in specific objects you could save that all and just do `Objects.First().Dipartimento.AreeDipartimento...` – Tim Schmelter Sep 10 '15 at 08:19

3 Answers3

3

The whole query makes not much sense to me. You are grouping by FAC_ID, then you take an arbitrary first of each group(no order), from those you take an arbitrary object. You could achieve the same more efficient and clearer:

var obj = Objects.FirstOrDefault();

Maybe you want this instead(just guessing):

MyRepeater.DataSource = Objects
    .GroupBy(p => p.FAC_ID)
    .Select(grp => grp.First())  // remove duplicates, keep an arbitrary
    .SelectMany(p => p.Dipartimento.AreeDipartimento.Select(ad => ad.Aree));

Here SelectMany selects the Aree object from the sequence Dipartimento.AreeDipartimento and flattens them all to a single sequence which is used as DataSource for the repeater.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

You should use .SelectMany if you are joining multiple sequences and, therefore, the argument must be a sequence. If that isn't the case, stick with .Select method.

For more information, take a look here: Difference Between Select and SelectMany

Community
  • 1
  • 1
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
0

I think you want to use Select here instead of SelectMany.

SelectMany is used to select a collection in the object you are handling in your query. The selected collections are then joined together in the result.

For example if you got something like this:

var a = {Tuple.Create("a", {1, 2, 3}),
         Tuple.Create("b", {4, 5, 6}),
         Tuple.Create("c", {7, 8, 9})}

You could use SelectMany to get all the result values in Item2 of the Tuple joined in one list. a.SelectMany(t => t.Item2) would result in

{1, 2, 3, 4, 5, 6, 7, 8, 9}

Select on the other hand is used to mark exactly one result (and I assume that this is what you want. So a.Select(t => t.Item1) would result in {"a", "b", "c"} and a.Select(t => t.Item2) would result in:

{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

So all in all I think in your case something like

MyRepeater.DataSource = Objects.GroupBy(p => p.FAC_ID).Select(p => p.First())
                               .First().Dipartimento.AreeDipartimento
                               .Select(p => p.Aree);

would be correct. This is all the tips I can give you without additional information regarding your data structure and your expected results.

Nitram
  • 6,486
  • 2
  • 21
  • 32