LINQ join(?) question

P

PJ6

I can't seem to figure out how to do this -

class A
integers as list(of integer)
end class

If I have a collection of class A objects, is it possible to, using LINQ,
get an aggregate of all the integers contained in their integers
collections, as an enumerable of integer?

Paul
 
C

Cowboy \(Gregory A. Beamer\)

As you have it set up, you can do the following

Dim intAnswer as Integer = (from i in integers return i).Sum()

For an array of integers, it is even simpler:

Dim intAnswer as Integer = integers.Sum()

Disclaimer: Just typed in the syntax and did not run a test on it. Should
only require a small fix, if any.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Blog:
http://feeds.feedburner.com/GregoryBeamer

*********************************************
| Think outside the box |
*********************************************
 
M

Michael C

Cowboy (Gregory A. Beamer) said:
As you have it set up, you can do the following

Dim intAnswer as Integer = (from i in integers return i).Sum()

For an array of integers, it is even simpler:

Dim intAnswer as Integer = integers.Sum()

This will work for list also.

Michael
 
C

Cor Ligthert[MVP]

Gregory,

The first one does not work, can you explain this one a little bit?

Cor
 
C

Cor Ligthert[MVP]

Hi,

Something I got from somebody else.

Dim x = New Integer() {1, 2, 3}.Sum()

Cor
 
M

Michael C

Cor Ligthert said:
Gregory,

The first one does not work, can you explain this one a little bit?

He meant to write "select" instead of "return". He did say it was likely to
have typos :)

Michael
 
C

Cor Ligthert[MVP]

Can you then eleborate a little bit more what you mean

Giving a correct answer is almost impossible as the OP does not explain
right. Even your sample has a major error.

(But probably you give us the fun to write as somebody else answers
whatever: "Oh you understand me, only your answer is correct")

Cor
 
A

Andrew Morton

PJ6 said:
I did not ask for a sum, that's trivial. I asked for an enumerable.

Yes, well, just because you ask a question that doesn't mean you'll get
answers to /that/ question ;-) Give us a LINQ to a red herring and away we
go!

But seriously, what do you want to use the enum for? How does the integer
correspond to the enum, given that enums are integers anyway, and simply
syntactic sugar to make it easier to program.

Do you actually want an array (or list) of integers?

Andrew
 
P

PJ6

OK, let's say an array, then, for clarity.

The choice of integer as a data type was incidental; I could have picked
string or another class, but was trying to distill the question into its
simplest form. Maybe integer was a bad choice because people like to add them
:-\

Paul
 
B

Branco Medeiros

PJ6 wrote:
class A
    integers as list(of integer)
end class

If I have a collection of class A objects, is it possible to, using LINQ,
get an aggregate of all the integers contained in their integers
collections, as an enumerable of integer?

Hi, Paul.

You probably want to use the SelectMany extension.

<example>
Dim YourAs As New List(Of A) 'pun *intended* =))
'... Fill YourAs with new A's
'...
'...
'Flatten all the integers into one big list
Dim FlattenedInts As IEnumerable(Of Integer) = _
YourAs.SelectMany(Function(MyA As A) MyA.integers))

For Each I As Integer In FlattennedInts
'... do whatever you please with each integer
Next
</example>

HTH.

Regards,

Branco.
 
M

Michael C

PJ6 said:
I can't seem to figure out how to do this -

class A
integers as list(of integer)
end class

If I have a collection of class A objects, is it possible to, using LINQ,
get an aggregate of all the integers contained in their integers
collections, as an enumerable of integer?

Ahhhh!!!! I just realised what you're actually asking (I think!!). Try this

MyCollection.Select(function(a) a.integers.Sum())

Michael
 
M

Michael C

PJ6 said:
OK, let's say an array, then, for clarity.

The choice of integer as a data type was incidental; I could have picked
string or another class, but was trying to distill the question into its
simplest form. Maybe integer was a bad choice because people like to add
them
:-\

See my answer to your original question. If that is not what you want can
you please explain the question again. There is obviously a lot of confusion
and you're doing little to ease that confusion with your short cryptic
replies. :)

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top