Using SelectMany

1,481 views
Skip to first unread message

Monkey

unread,
Jul 26, 2009, 5:26:06 PM7/26/09
to DbLinq
I'm having troubles using SelectMany in this manner:

var allTeams = from t in leagues.SelectMany( l => l.Teams )
select t;

I get the following error:

The type arguments for method
'System.Linq.Enumerable.SelectMany<TSource,TResult>
(System.Collections.Generic.IEnumerable<TSource>,
System.Func<TSource,int,System.Collections.Generic.IEnumerable<TResult>>)'
cannot be inferred from the usage. Try specifying the type arguments
explicitly.

Does anyone know a way around this? I'd be happy to specify the type
arguments explicitly as it asks, but I'm not really sure how that's
done.

Thanks :)

Jonathan Pryor

unread,
Jul 27, 2009, 10:03:58 AM7/27/09
to dbl...@googlegroups.com

To specify type argument explicitly, do:

leagues.SelectMany<LeaguesType, TeamType>(l => l.Teams)

- Jon


Monkey

unread,
Jul 28, 2009, 8:19:48 PM7/28/09
to DbLinq
Thanks for the reply Jon. This helps a bit.
I'm having problems getting parent-child relationships to work now
though.
I have a single table with a "parent" column that relates to the "id"
column. I can't work out how to query that to get all the children of
an item.
Linq documentation seems to suggest I can create relationships with
the DataLoadOptions but I can't seem to get it working..

At the moment I have this, which is horribly inefficient:

List<uint> parents = new List<uint>();
uint current = obj;

while (current > 1){
var parent = from node in db.Objects
where node.OID == current
select node.Parent;
parents.Add(parent.First());
current = parent.First();
}
return parents;

Does anyone have an efficient way of doing this that actually works in
dbLinq?

Thanks

Sami M. Kallio

unread,
Jul 28, 2009, 9:08:34 PM7/28/09
to dbl...@googlegroups.com
If you are asking how you get the child items of a parent, then:

1) Define the foreign key constraint in your database table
2) Run Dbmetal to create your DbLinq DataContext class

You database class should have both the Parent and Child tables now, with
the Parent having an EntitySet<Child> Child in the Children region. You can
then go through them like any other IEnumerable class:

foreach (Child _child in Parent.Child)
{
...
}

Should be that simple, unless of course I misunderstood the
question/problem.

- Sami

Reply all
Reply to author
Forward
0 new messages