将where子句应用于LightSwitch查询中的childcollection [英] Applying where clause to childcollection in LightSwitch query

查看:59
本文介绍了将where子句应用于LightSwitch查询中的childcollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在针对子集合的LightSwitch查询中使用where子句?

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

我加入了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:

错误2

无法将方法组"SingleOrDefault"转换为非委托类型"System.Linq.IQueryable".您打算调用该方法吗?

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;
}

返回IQueryable,它是一个集合,而不是单个集合.

Return IQueryable which is a collection not single one .

我尝试将签名更改为部分无效的Query1_PreprocessQuery(参考博客查询),现在又遇到另一个编译错误,说博客不包含针对何处的定义.

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.

因此,如何在LightSwitch HTMLClient应用程序中实现所需的查询类型.

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

推荐答案

为了组织这种类型的标量查询,您需要在查询的预处理方法中使用以下类型的LINQ表达式:

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

以下Microsoft博客中标量​​查询"部分介绍了这种方法:

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

Visual Studio LightSwitch小组博客:如何创建组合和标量查询(Ravi Eda)

尽管此博客中的屏幕示例均基于Silverlight客户端,但其涵盖的查询技术也同样适用于HTML客户端.

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.

这篇关于将where子句应用于LightSwitch查询中的childcollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆