如何使用数据透视表与实体框架? [英] How to use Pivot table with Entity Framework?

查看:276
本文介绍了如何使用数据透视表与实体框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张这样的表:

我想使用将使用存储过程的EF,这将返回一个不透明的结果集。但问题是,我如何建模,以便我可以在RIA服务中使用它将数据推送到客户端。我正在去做这样的事情

I would like to use EF which will use a stored procedure which will return a unpivot result set back. But the problem is, how would I model it so that I can use it in RIA services to push the data to client. I was going for something like this

public class RegionModel {
    public string Name { get; set; }
    public List<string> Quarter { get; set; }
    public List<int> Sales { get; set; }
}

同样的方式将会有一个 QuarterModel 以及。根据用户选择,我可以返回正确的绑定集合。目前我们通过在客户端创建动态类来解决这个问题。但是有兴趣的是,无需客户端代码就可以实现这一点,并且可以利用EF和SQL Server不起作用。

Same way there will be a QuarterModel as well. Based on the user selection, I can return the proper collection for binding. Currently we have solved this by creating dynamic class at the client side. But interested to know is it possible to achieve this without client side code and leverage the EF and SQL Server unpivot.

推荐答案

透视功能可将对象集合转换为平铺原始层次结构的新对象集合。
在下面的示例中,数据透视扩展方法用于转换集合,如下所示:

The pivot feature enables transforming a collection of objects into a new collection of objects flattening the original hierarchy. In the following example The Pivot extension method is used to transform a collection as follow:

使用以下链接:

LINQ扩展程序库(数据透视扩展)

使用以下行来转发数据:

Using the following line to pivot the data:

contacts.Pivot(X => X.Phones, X => X.PhoneType, X => string.Concat("(", X.AreaCode, ") ", X.PhoneNumber), true)

这篇关于如何使用数据透视表与实体框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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