Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - LINQ - Concatenated seq from subcollection under parent collection

Reply
 
Old 03-04-2008   #1 (permalink)
Todd Beaulieu


 
 

LINQ - Concatenated seq from subcollection under parent collection

I suspect this is super simple, but I'm just getting started with linq.

I have a collection, say Orders. Under each Order I have a collection of
Lines.

I want to get a distinct list of ItemNumbers for all Orders.

Does anyone know how to do this? Is it a SelectMany()?

Thank you.

My System SpecsSystem Spec
Old 03-04-2008   #2 (permalink)
Jon Skeet [C# MVP]


 
 

Re: LINQ - Concatenated seq from subcollection under parent collection

Todd Beaulieu <ToddBeaulieu@xxxxxx> wrote:
Quote:

> I suspect this is super simple, but I'm just getting started with linq.
I almost envy you, having the wonderful journey of discovery still
almost entirely ahead of you. (That's not to say I've stopped learning
about LINQ - far from it.)
Quote:

> I have a collection, say Orders. Under each Order I have a collection of
> Lines.
>
> I want to get a distinct list of ItemNumbers for all Orders.
>
> Does anyone know how to do this? Is it a SelectMany()?
I'm assuming we have a situation like this:

public class Order
{
public IEnumerable<Line> Lines { get; set; }
}

public class Line
{
public int ItemNumber;
public int Quantity { get; set; }
}

and you have an IEnumerable<Order> to examine.

You want to end up with an IEnumerable<int> which consists of all the
ItemNumbers of all the orders, but distinct. In other words, "all the
items we've sold *any* of". Is that about right?

The simplest way to do this is to use SelectMany(), as you've
suggested. That's usually more easily expressed in a query expression.
So, we'd have:

var allItems = from order in orders
from line in order.Lines
select line.ItemNumber;

var distinctItems = allItems.Distinct();


So, allItems effectively flattens the sequence of item numbers, and
then the second statement finds the distinct items. The query
expression boils down to this:

var allItems = orders.SelectMany (order => order.Lines,
(order, line) => line.ItemNumber);

Does that help? Let me know if I've missed the boat, and I'll have
another go

--
Jon Skeet - <skeet@xxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
My System SpecsSystem Spec
Old 03-04-2008   #3 (permalink)
Todd Beaulieu


 
 

Re: LINQ - Concatenated seq from subcollection under parent collec

Jon, you ROCK!

Thanks, bud. That was exactly what I needed.

It's funny about the "envying" bit. I *am* quite excited lately about all
these new wonderful technologies I'm learning.

After getting a simple, but nonetheless cool LINQ query going in my app, and
thus avoiding the usual enumeration mess, I actually looked for someone to
share the moment with. Sadly, nobody there would have cared.

I will certainly need to study your message some more so I can understand it.

Thanks again!

"Jon Skeet [C# MVP]" wrote:
Quote:

> Todd Beaulieu <ToddBeaulieu@xxxxxx> wrote:
Quote:

> > I suspect this is super simple, but I'm just getting started with linq.
>
> I almost envy you, having the wonderful journey of discovery still
> almost entirely ahead of you. (That's not to say I've stopped learning
> about LINQ - far from it.)
>
Quote:

> > I have a collection, say Orders. Under each Order I have a collection of
> > Lines.
> >
> > I want to get a distinct list of ItemNumbers for all Orders.
> >
> > Does anyone know how to do this? Is it a SelectMany()?
>
> I'm assuming we have a situation like this:
>
> public class Order
> {
> public IEnumerable<Line> Lines { get; set; }
> }
>
> public class Line
> {
> public int ItemNumber;
> public int Quantity { get; set; }
> }
>
> and you have an IEnumerable<Order> to examine.
>
> You want to end up with an IEnumerable<int> which consists of all the
> ItemNumbers of all the orders, but distinct. In other words, "all the
> items we've sold *any* of". Is that about right?
>
> The simplest way to do this is to use SelectMany(), as you've
> suggested. That's usually more easily expressed in a query expression.
> So, we'd have:
>
> var allItems = from order in orders
> from line in order.Lines
> select line.ItemNumber;
>
> var distinctItems = allItems.Distinct();
>
>
> So, allItems effectively flattens the sequence of item numbers, and
> then the second statement finds the distinct items. The query
> expression boils down to this:
>
> var allItems = orders.SelectMany (order => order.Lines,
> (order, line) => line.ItemNumber);
>
> Does that help? Let me know if I've missed the boat, and I'll have
> another go
>
> --
> Jon Skeet - <skeet@xxxxxx>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> World class .NET training in the UK: http://iterativetraining.co.uk
>
My System SpecsSystem Spec
Old 03-04-2008   #4 (permalink)
Jon Skeet [C# MVP]


 
 

Re: LINQ - Concatenated seq from subcollection under parent collec

Todd Beaulieu <ToddBeaulieu@xxxxxx> wrote:
Quote:

> Jon, you ROCK!
>
> Thanks, bud. That was exactly what I needed.
>
> It's funny about the "envying" bit. I *am* quite excited lately about all
> these new wonderful technologies I'm learning.
I can't remember the last time I found a technology suite this
intoxicating. It's clearly useful, *and* fun to play with. It's easy to
come up with examples to write about, and they can be thoroughly
bizarre (check my blog for specimens).
Quote:

> After getting a simple, but nonetheless cool LINQ query going in my app, and
> thus avoiding the usual enumeration mess, I actually looked for someone to
> share the moment with. Sadly, nobody there would have cared.
I remember the first time I used an iterator block - I showed a few
people, but I don't think any of them "got" quite how cool it was.
Quote:

> I will certainly need to study your message some more so I can understand it.
Just let me know if I can help explain it further. This isn't a
selfless act - I want to learn how to express LINQ ideas as easily as
possible, hence Human LINQ and Visual LINQ. I think I've expressed the
C# side of LINQ reasonably clearly in my book, but that's no good for
newsgroup posts. (I refuse to turn *every* LINQ thread into a plug for
the book.)

Basically, if you find one particular part of a post difficult, it
could be for any or all of three reasons:

1) I've fouled up the explanation
2) It's a fundamentally tricky concept
3) You're not really reading what I've written

1 and 2 are more likely than 3, and 1 is often fixable.

--
Jon Skeet - <skeet@xxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
getting cr/lf out of xml with linq to xml? .NET General
Linq to SQL .NET General
Parent Process ID PowerShell
Get parent directory PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46