Diego Stiehl
Diego Stiehl

Reputation: 403

LINQ select with IEnumerable

I'm using EF 4.

I have an IEnumerable<Type01> where each of the items (of Type01) has an IEnumerable<Type02>.

This can explain:

Type01 objType01 = ...;
IEnumerable<Type02> en = objType01.allObjType02;

I need to do (using LINQ) a select that gives me an IEnumerable<Type01> as result, but the "record count" must be the same of the sum of "record count" of all Type02 items.

For example. For this list:

myItem01a
    myItem02a
    myItem02b
myItem01b
    myItem02c
    myItem02d
    myItem02e

The select return must be:

myItem01a
myItem01a
myItem01b
myItem01b
myItem01b

I know how to do this by using old school SQL (JOIN clause). But I'm fairly new to LINQ expressions. How could it be done?

Upvotes: 2

Views: 690

Answers (1)

AakashM
AakashM

Reputation: 63338

Cheat. Use SelectMany (or the equivalent multiple froms) to iterate over all Type02s, but only yield a Type01 each time round:

var repeatedType01s = 
    from t01 in enumerableOfType01s
    from t02 in t01.allObjType02
    select t01;

Now repeatedType01s is an IEnumerable<Type01> with the cardinalities you want.

Upvotes: 6

Related Questions