Follow navigation property in dynamic linq

c# dynamic-linq

Question

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

1
0
7/14/2016 2:20:06 PM

Accepted Answer

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.

0
7/14/2016 2:32:45 PM

Popular Answer

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 :)



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