markzzz
markzzz

Reputation: 47995

SelectMany can't deduce argument?

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?

Upvotes: 0

Views: 126

Answers (3)

Nitram
Nitram

Reputation: 6716

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.

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460228

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.

Upvotes: 3

Rubens Farias
Rubens Farias

Reputation: 57976

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

Upvotes: 0

Related Questions