“分组依据" PreprocessQuery中的解决方法 [英] "Group By" workaround in PreprocessQuery

查看:57
本文介绍了“分组依据" PreprocessQuery中的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!我的数据结构如下所示,包括我希望查询返回的结果.

Hello! My data structure looks as following, including the results I want my query to return.

Key       Revision
==================
abc       null
def       null
ghj       2
ghj       null
klm       null
def       1
abc       1
abc       2

Result if parameter is 1
========================
abc       1
def       1
ghj       null
klm       null

Result if parameter is null
========================
abc       null
def       null
ghj       null
klm       null


我的LINQ查询如下:

My LINQ query looks like this:

partial void RuleEntriesByUserSaveId_PreprocessQuery(int? UserSaveId, ref IQueryable<RuleEntry> query)
{
   query = query.Where(re => re.Revision == null || re.Revision == UserSaveId)
                .GroupBy(re => re.Key)
                .Select(g => g.FirstOrDefault(x => x.Revision != null) ?? g.First());
}

我显然不工作,因为LightSwitch PreprocessQuery不支持GroupBy,我知道我可以通过使用WCF RIA服务聚合数据来解决此问题,但我宁可避免这种麻烦.

I obviously don't work since GroupBy isn't supported in LightSwitch PreprocessQuery, I know I can solve this by aggregating data with a WCF RIA Service but I rather not get into that hassle.

我的问题是,是否可以通过某种方式在不使用GroupBy的情况下重写此查询?

My question is if I, in some way, could rewrite this query without using GroupBy?

非常感谢

推荐答案

另一种替代方法是SQL Server视图:

Another alternative is a SQL Server view:

1)在SQL Server中创建一个视图进行分组.

1) Create a view in SQL Server to do the grouping.

2)将视图添加到LightSwitch中的数据源.

2) Add the view to a data source in LightSwitch.

3)(可选)在LightSwitch中添加查询以添加更多过滤或排序.

3) Optionally, add a query in LightSwitch to add more filtering or sorting.

4)将查询或表格添加到屏幕上.

4) Add the query or table to your screen.

这非常快,除了创建视图外无需进行任何编程.

This is very quick and takes no programming other than creating the view.

标记


这篇关于“分组依据" PreprocessQuery中的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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