合并数据源值 asp.net
我想看看他们是否有任何可能的方式来组合数据源值。数据的结构是相同的,
Year| Amount Used| Amount Collected | Amount Saved
我想要做的是结合 IQueryable
数据类型T 是相同类型 的数据源,这将是这样的结构,然后将其绑定到 gridview。
我唯一不想添加在一起的列是年份,因为我希望根据年份合并源,而且每年可能不在数据源中。
这怎么可能,唯一的方法是为每个源创建一个 foreach 语句并手动组合它们吗?
我正在寻找更简单的东西
I am trying to see if their is any possible way to combine data source values. The structure of the data would be the same,
Year| Amount Used| Amount Collected | Amount Saved
What I want do do it combine data sources that are IQueryable<T>
data types T being the same type that would be this structure and then bind it to a gridview.
Only column I dont want to add together is the year, because I want the sources to combine based on the year, and also every year may not be in the data sources.
How is this possible, would the only way is making a foreach statement for each source and combine them manually?
I'm looking for something simplier
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 LINQ 的 Union 运算符组合两种 IQueryable 数据类型。我将使用联合运算的结果创建一个新变量。从那里按年分组并使用聚合函数(总和可能就是您正在寻找的)来执行计算。
编辑:这是一个例子。我会告诉你,我自己正在学习 LINQ,并且有很多教程(Google 是你的朋友)。我一直在 VB 中学习 LINQ,它的语法与 C# 有所不同。我尽力用 C# 编写示例,但它可能不完全正确,但会给您一个很好的起点。
如果您使用 LINQ to DataSets,则第一行可能需要替换为:
以下是我一直在使用的一些资源(Google 除外)。 Scott Gu 的博客(单击右侧的 LINQ 标记)。 4 个人也是一个很好的多部分系列。
Use LINQ's Union operator to combine the two IQueryable data types. I would create a new variable with the result of the union operation. From there group by year and use a aggregate function (sum would probably be what you are looking for) to perform the calculations.
EDIT: Here is an example. I will tell you I am in the process of learning LINQ myself and there are LOTS of tutorials out there (Google is your friend). I have been learning LINQ in VB which has a somewhat different syntax as C#. I did my best to write the example in C# but it may not be exactly correct but will give you a good starting point.
If the you are using LINQ to DataSets the first line may need to be replaced with:
Here are some of the resources (other than Google) that I have been using. Scott Gu's blog (on the right click the LINQ tag). 4 Guys has a good multipart series also.