How to use “SelectMany” with DataServiceQuery<>

微笑、不失礼 提交于 2019-12-01 07:55:43

Data Services doesn't support composing SelectMany with a subsequent Select unless you include a key selector to filter the 'Many' back to just one item.

If you think about queries in terms of URIs you will understand why.

In OData URIs you have to have just one Entity before you navigate (i.e. /NavigationProperty).

So this:

~/Users(123)/ConsumerXRef

is okay because you have one User (123) before you retrieve the many related ConsumerXRef(s).

However this is no good:

~/Users(123)/ConsumerXRef/Account

because you don't identify a single ConsumerXRef before navigating to accounts.

When you take this thinking into LINQ land, something like this:

from u in ctx.Users
where u.ID == 123
from c in u.ConsumerXRef
select c;

is okay because it roughly translates to:

~/Users(123)/ConsumerXRef

But this:

from u in _gsc.Users
where u.UserId == myId
from c in u.ConsumerXref
where c.AccountName == "MyAccount" && c.IsActive
select x.Account;

is no good because - I'm guessing here - AccountName isn't the key? so this translates to something like this URL

~/Users(123)/ConsumerXRef/Account/?$filter=AccountName eq 'MyAccount' ...

which is invalid because you've navigated (from ConsumerXRefs to their Accounts) without first selecting a specific ConsumerXRef.

Does this make sense?

Hope so

Alex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!