How to use Pivot table with Entity Framework?

旧城冷巷雨未停 提交于 2019-12-10 20:22:36

问题


I have a table something like this:

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

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.


回答1:


Answer :

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:

Using the folowing Link:

LINQ Extensions Library (Pivot Extensions)

Using the following line to pivot the data:

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



回答2:


@cadrell0 is correct, it is view side code. I was able to achieve that using generic dynamic objects. I recently blogged about it @ http://csharprambling.wordpress.com/2011/11/23/expandoobject-in-silverlight-take-2/



来源:https://stackoverflow.com/questions/8082057/how-to-use-pivot-table-with-entity-framework

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