合并数据源值 asp.net

发布于 2024-12-17 13:50:24 字数 347 浏览 3 评论 0原文

我想看看他们是否有任何可能的方式来组合数据源值。数据的结构是相同的,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情仇皆在手 2024-12-24 13:50:24

使用 LINQ 的 Union 运算符组合两种 IQueryable 数据类型。我将使用联合运算的结果创建一个新变量。从那里按年分组并使用聚合函数(总和可能就是您正在寻找的)来执行计算。

编辑:这是一个例子。我会告诉你,我自己正在学习 LINQ,并且有很多教程(Google 是你的朋友)。我一直在 VB 中学习 LINQ,它的语法与 C# 有所不同。我尽力用 C# 编写示例,但它可能不完全正确,但会给您一个很好的起点。

var unionResult = dataSet1.Union(dataSet2);

var groupedResult = from r in unionResult
        group r By r.Year into g
        Select new
        {
            Year = g.Key.Year,
            TotalUsed = g.Sum(r => r.AmountUsed),
            TotalCollected = g.Sum(r => r.AmountCollected),
            TotalSaved = g.Sum(r => r.AmountSaved
        };

如果您使用 LINQ to DataSets,则第一行可能需要替换为:

var unionResult = dataSet1.AsEnumerable().Union(dataSet2.AsEnumerable());

以下是我一直在使用的一些资源(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.

var unionResult = dataSet1.Union(dataSet2);

var groupedResult = from r in unionResult
        group r By r.Year into g
        Select new
        {
            Year = g.Key.Year,
            TotalUsed = g.Sum(r => r.AmountUsed),
            TotalCollected = g.Sum(r => r.AmountCollected),
            TotalSaved = g.Sum(r => r.AmountSaved
        };

If the you are using LINQ to DataSets the first line may need to be replaced with:

var unionResult = dataSet1.AsEnumerable().Union(dataSet2.AsEnumerable());

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文