报告中的自定义聚合函数

发布于 2024-08-08 09:45:15 字数 956 浏览 3 评论 0原文

我有每个类别的产量每日记录,即A类:80吨。每个报告行将如下所示:

             Daily                  Week to Date          Month to Date
             Actual  Plan   Var     Actual  Plan   Var    Actual   Plan   Var
Category A   100     110    -10     230      300   -70    900      1200  -300
Category B   etc. etc.

类别 A:在一周的第 2 天,开采了 10 桶矿石。按 10 吨/料斗计算,这等于您在上面“每日实际”下看到的 100 吨。因此,对于 A 类,我需要一个定义为“输入 * 10”的公式,但它并不总是一个简单的乘法因子。我需要存储输入的任何公式 ->每日图。在第一天,我的实际重量为 132 吨,但已知有 2 吨/天的正误差,因此本周迄今为止的实际重量不仅仅是一个简单的总和,需要向下调整。因此,我需要一个特殊的周至今实际值公式,例如 (d1+...+dn) - 2n。

B 类:第 2 天,化验结果显示每吨矿石含有 x 公斤镍。对于 B 类,我需要镍/矿石 * 100 的每日实际公式,但同样,我必须能够将任何公式分配给 B 类的每日实际值。对于本周迄今为止的实际值,我是否使用平均值、中位数或模式,有调整吗?

在我的类别数据库表中,我有例如

类别 A: UnitOfMeasure=Tons, DailyFormula="input*10",WeeklyFormula="(d1+...+dn) - 2n"

我的代码需要在以下情况下应用类别 A 的唯一公式:计算报告中 A 类行的值。我想尝试将其转化为针对原始数据的 LINQ 查询。

编辑:我需要一种方法来让用户定义这些公式,并根据存储在数据库列中的公式在运行时动态解析和评估它们。

I have daily records of production per category, i.e. category A: 80 tons. Each report row will look like this:

             Daily                  Week to Date          Month to Date
             Actual  Plan   Var     Actual  Plan   Var    Actual   Plan   Var
Category A   100     110    -10     230      300   -70    900      1200  -300
Category B   etc. etc.

Category A: On day 2 of the week, 10 skips of ore are mined. At 10 tons/skip, that equals the 100 tons you see above, under Daily Actual. So, for Category A, I need a formula defined that says "input * 10", but it won't always be a simple multiplication factor. I need to store any formula for the input -> Daily figure. On Day 1, I have a 132 ton Actual, but a positive error of 2 tons/day is known, so the Week to Date Actual is not just a simple sum and needs to be adjusted down. So I need a special formula for Week to Date actuals, like (d1+...+dn) - 2n.

Category B: On day 2, an assay result gives x kilograms of nickel per ton of ore. For Category B, I need a daily Actual formula of nickel/ore * 100, but again, I must be able to assign any formula to Daily Actuals for Category B. For the Week to Date Actuals, do I use mean, median, or mode, and is there an adjustment?

In my Categories DB table, I have e.g.

Category A: UnitOfMeasure=Tons, DailyFormula="input*10",WeeklyFormula="(d1+...+dn) - 2n"

My code needs to apply the unique formulae for Category A when calculating values for a Category A line in the report. I want to try and work this into a LINQ query against the raw data.

EDIT: I need a way to let the user define these formulae, and to dynamically parse and evaluate them at run time, based on the formula stored in a DB column.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

彼岸花似海 2024-08-15 09:45:15

我希望我理解正确。我对自定义分组的想法是通过从类别到分组方法的映射来使用的。

我对其进行了一些简化,以使答案更加清晰:

假设报告输入的行属于此类:

public class Production
{
    public string Category { get; private set; }
    public int DayIndex { get; private set; }
    public double Input { get; private set; }
}

在此类中,我们有类别、当天的索引以及可以表示开采矿石量等的输入。

Production[] results = // The inputs to the report

// The map from category to the grouping method
var groupingMethods =
    new Dictionary<string, Func<IGrouping<string, Production>, double>>
      {
          {
              "ore", // Category name
              grouping =>                                        // The method
              grouping.Sum(production => production.Input) * 10  // grouping ore 
          },                                                     // rows
          {
              "nickel",
              grouping =>
              grouping.Sum(production => production.Input) - 2 * grouping.Count()
          }
      };

// The query create groups based on the category
var report = from result in results
             group result by result.Category
             into categoryResults
             select new
                  {
                     // Created anonymous object with Category
                     Category = categoryResults.Key,
                     // Find the grouping method of the category, invokes it
                     // and store the result in Week column
                     Week = groupingMethods[categoryResults.Key](categoryResults)
                  };

这里的主要思想是“切换”方法取决于类别。可以轻松地将其更改为按其他标准进行分组,当然还可以添加和更改分组。此外,对于其他列,可以通过为新列添加另一个字典来轻松调整。

例如:

var weekMethods =
    new Dictionary<string, Func<IGrouping<string, Production>, double>>() ...

var monthMethods =
    new Dictionary<string, Func<IGrouping<string, Production>, int>>() ...


from result in results
group result by result.Category
into categoryResults
select new
       {
           Category = categoryResults.Key,
           Week = weekMethods[categoryResults.Key](categoryResults),
           Month = monthMethods[categoryResults.Key](categoryResults)
       };

I hope I understood correctly. The idea I have for custom grouping is used by map from category to grouping method.

I have simplified it a little to make the answer clear:

Let's say the rows of the report input are of this class:

public class Production
{
    public string Category { get; private set; }
    public int DayIndex { get; private set; }
    public double Input { get; private set; }
}

In this class we have category, the index of the day and the input that can represent for example the amount of ore mined.

Production[] results = // The inputs to the report

// The map from category to the grouping method
var groupingMethods =
    new Dictionary<string, Func<IGrouping<string, Production>, double>>
      {
          {
              "ore", // Category name
              grouping =>                                        // The method
              grouping.Sum(production => production.Input) * 10  // grouping ore 
          },                                                     // rows
          {
              "nickel",
              grouping =>
              grouping.Sum(production => production.Input) - 2 * grouping.Count()
          }
      };

// The query create groups based on the category
var report = from result in results
             group result by result.Category
             into categoryResults
             select new
                  {
                     // Created anonymous object with Category
                     Category = categoryResults.Key,
                     // Find the grouping method of the category, invokes it
                     // and store the result in Week column
                     Week = groupingMethods[categoryResults.Key](categoryResults)
                  };

The main idea here is the "switching" methods depends on the category. It can be easily changed to group by other criteria and of course add and change the groupings. In addition, for additional columns it can easily adjust by adding another dictionary for the new column.

For example:

var weekMethods =
    new Dictionary<string, Func<IGrouping<string, Production>, double>>() ...

var monthMethods =
    new Dictionary<string, Func<IGrouping<string, Production>, int>>() ...


from result in results
group result by result.Category
into categoryResults
select new
       {
           Category = categoryResults.Key,
           Week = weekMethods[categoryResults.Key](categoryResults),
           Month = monthMethods[categoryResults.Key](categoryResults)
       };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文