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