Applying where clause to childcollection in LightSwitch query

依然范特西╮ 提交于 2019-12-13 05:26:02

问题


How can I have a where clause in a LightSwitch query against a child collection?

In my example, every blog can have many comments and I want to write a query like this so that the admin see only the blog entries without any comments:

query = query.Where(q => q.comment.Count() == 0).SingleOrDefault()

I've included the SingleOrDefault() because I want the administrator to see only one item per query they can then add a comment and move to next item on the blog screen.

When trying the above query, I get the following compile time error:

Error 2

Cannot convert method group 'SingleOrDefault' to non-delegate type 'System.Linq.IQueryable'. Did you intend to invoke the method?

I thinks this is the right error because:

partial void Query1_PreprocessQuery(ref IQueryable<blog> query)
{
    query=query.Where(q => q.Evaluations.Count() == 0).SingleOrDefault;
}

Return IQueryable which is a collection not single one .

I've tried changing the signature to partial void Query1_PreprocessQuery(ref blog query) and now I get another compile error saying blog does not contain a definition for where.

So, how can I implement the required type of query in my LightSwitch HTMLClient app.


回答1:


In order to organise this type of scalar query, you need to use the following type of LINQ expression in your query's pre-process method:

partial void BlogWithNoComment_PreprocessQuery(ref IQueryable<blog> query)
{
    query = query.Where(q => q.comment.Count() == 0).Take(1);
}

In addition, you'll need to ensure your query is designed to only return a single result by selecting the 'One' option in the query's 'Number of Results Returned' property as follows (the required property selection is highlighted in red):

This type of approach is covered in the following Microsoft blog in the Scalar Queries section:

Visual Studio LightSwitch Team Blog: How to Create Composed and Scalar Queries (Ravi Eda)

Even though the screen examples in this blog are based on the Silverlight client, the query techniques it covers apply equally to the HTML client.



来源:https://stackoverflow.com/questions/34412291/applying-where-clause-to-childcollection-in-lightswitch-query

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