0

I'm trying to use dynamic linq to query data. Now I have a (normal) linq query like:

from u in c.Users
from d in u.Documents
select d.DocumentID

I'm looking for the equivalent of this in dynamic linq. The emphasis is on how to navigate from the User entity to the Document entity. I can do something like:

c.Users.Select("new (UserName)");

But of course i cannot do:

c.Users.Select("new (Documents.DocumentID AS DocumentID)");

I thought i might be able to do something like:

var q = c.Users.Select("Documents");
q.Select("new (DocumentID)");

But this doesn't work.

I've found tons of examples of using navigation properties in where clauses. I've been able to use them in where clauses, but not in the select.

I've found one example of someone doing:

c.Users.SelectMany("Documents").Select("new (DocumentID)");

However, dynamic linq doesn't seem to have support for selectMany. At least there doesn't seem to be any overload to selectmany accepting a string as input when dynamic linq is included.

Is there anyone who can push me into the right direction? I would also welcome suggestions on how to do the selectmany without dynamic linq as well, as long as the "Documents" part and the select list (in this case "DocumentID" is dynamic and can be determined by a string input

PaulVrugt
  • 1,682
  • 2
  • 17
  • 40

2 Answers2

0

Ok

It seems that I was on the right track. I've included a different version of Dynamic linq, being: System.Linq.Dynamic.Core

This version of dynamic linq actually has support for selectmany in it. The option:

c.Users.SelectMany("Documents").Select("new (DocumentID)");

then works.

PaulVrugt
  • 1,682
  • 2
  • 17
  • 40
0

Try

var ids = Users.SelectMany(usr => usr.Documents.Select(i=>i.DocumentId));

var docs = Users.SelectMany(usr => usr.Documents);

Lambdas are really usefull and easy to learn. Compile-time safety is a plus :)

Questions are welcome :)

Jorge Rojas
  • 485
  • 3
  • 10
  • The whole point of using dynamic linq is that i don't know what the property names are :) so using lambdas is not an option – PaulVrugt Jul 14 '16 at 15:33