Follow navigation property in dynamic linq

c# dynamic-linq


I'm attempting to query data using dynamic linq. I now have a (typical) linq question like this:

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

I'm trying to find dynamic linq's equivalent of this. The focus is on the navigation process between the User object and the Document entity. I can do the following:

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

Of course, I am unable to:

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

I believed I may be able to perform the following:

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

But this is ineffective.

There are several instances of where navigation characteristics have been used. In where clauses I have been able to utilize them, but not in the select.

I discovered one instance of someone doing:

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

SelectMany doesn't seem to be supported by dynamic linq, however. When dynamic linq is used, selectmany doesn't seem to be overloaded in any way that accepts a string as input.

Do you know of somebody who could steer me in the proper direction? As long as the "Documents" portion and the choose list (in this example, "DocumentID" is dynamic and may be chosen by a text input") can be done without dynamic linq, I would also be open to recommendations on how to handle the selectmany.

7/14/2016 2:20:06 PM

Accepted Answer


I believe I was heading in the correct direction. I've added System.Linq.Dynamic.Core as an alternative to Dynamic Linq.

Selectmany is really supported by this version of dynamic linq. The choice:

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

then operates

7/14/2016 2:32:45 PM

Popular Answer


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

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

Lambdas are quite helpful and simple to understand. A benefit is safety at compile time.

Any inquiries are welcome:)

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow