去 .Take() 还是不去 .Take(),这就是问题

发布于 2024-10-26 07:37:35 字数 1356 浏览 1 评论 0原文

我有一个代表每个年龄段的人口(M,F)的集合。

为了预测随时间变化的人口,我必须首先对女性进行计算,以便我可以根据男性出生率的统计常数来计算新生男性和女性的百分比。

也就是说,我有第一个矩阵,其中包含每年的男性和女性人口。

// Where 1 is the evaluation Id and 2009 the year I want the 
// initial population information for.
IList<AnnualPopulation> initialPopulation = 
    LoadMatrix<AnnualPopulation>(1, 2009);

现在,为了首先预测女性人口,我使用以下方法:

IList<AnnualPopulation> initialWomenPopulation = (
        from w in initialPopulation
        where String.Equals("F", w.Gender)
        select w
    ).FirstOrDefault();

并且我必须考虑当前(第一年(2009))和我希望预测的年份的死亡率,我已经有死亡率各年龄的费率。

IList<AnnualDeathRate> deathRates = LoadMatrix<AnnualDeathRate>(1, 2009)

这加载了 2009 年及以后各年龄阶段的死亡率。因此,为了只考虑 2009 年和 2010 年,我使用了以下 Linq 代码。

AnnualDeathRate[] femaleDeathRates = (
        from fdr in deathRates
        where (string.Equals("F", fdr.Gender))
        select fdr
    ).TakeWhile(dr => 
        initialWomenPopulation.Year == dr.Year || 
        dr.Year == initialWomenPopulation.Year + 1
    ).ToArray()

问题

  1. 如果我在 dr.Year 上使用 .Take(2) 以及补充 where 条件,效果会更好吗? ?
  2. 还有更好的选择吗?
  3. 我的方法值得考虑吗?

I have a collection that represents the population (M, F) for each years of age.

In order to project population through time, I have to first make the calculations with the women so that I may calculate the percentage of new born men and women based on a statistical constant of masculinity birth rate.

That said, I have a first matrix which contains both men and women population per year of age.

// Where 1 is the evaluation Id and 2009 the year I want the 
// initial population information for.
IList<AnnualPopulation> initialPopulation = 
    LoadMatrix<AnnualPopulation>(1, 2009);

Now, in order to project the women's population first, I use the following:

IList<AnnualPopulation> initialWomenPopulation = (
        from w in initialPopulation
        where String.Equals("F", w.Gender)
        select w
    ).FirstOrDefault();

And I have to consider mortality rates for both the current (initial year (2009)) and the year for which I wish to project, from which I already have mortality rates for each year of age.

IList<AnnualDeathRate> deathRates = LoadMatrix<AnnualDeathRate>(1, 2009)

This loads the deather rates from 2009 and later for each year of age. Hence, in order to only consider 2009 and 2010, let's say, I used the following Linq code.

AnnualDeathRate[] femaleDeathRates = (
        from fdr in deathRates
        where (string.Equals("F", fdr.Gender))
        select fdr
    ).TakeWhile(dr => 
        initialWomenPopulation.Year == dr.Year || 
        dr.Year == initialWomenPopulation.Year + 1
    ).ToArray()

Questions

  1. Would it have been best if I had used .Take(2) along with a supplemental where condition on the dr.Year?
  2. Are there any better alternatives?
  3. Is my approach worth considerations?

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

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

发布评论

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

评论(2

埖埖迣鎅 2024-11-02 07:37:35

实际上#1 可能会产生与当前代码不同的结果。例如,如果顺序是随机的(我在任何地方都看不到任何排序),当前代码可能会返回任意数量的结果(例如,如果第一个元素不匹配任一条件,则没有结果,即使后续元素匹配)

听起来像方法 #1 会更正确(除非您实际上想要 TakeWhile 的行为)您是说您想要匹配给定条件的前 2 个结果,无论它们是否是第一个结果初始清单。

例如

var numbers = new[] {2, 3, 1, 4};
Console.WriteLine("TakeWhile");

foreach(var n in numbers.TakeWhile(x => x == 1 || x == 2))
{
    Console.Write(n);
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("WhereTake2");

foreach (var n in numbers.Where(x => x == 1 || x == 2).Take(2))
{
    Console.Write(n);
} 

打印:

TakeWhile
2

WhereTake2
21

Actually #1 may produce a different result than your current code. For instance if the order was random (I don't see any sorting anywhere) the current code may return any number of results (e.g. no results if the first element doesn't match either condition, even if subsequent ones do)

It sounds like approach #1 would be more correct (unless you actually want the behavior of TakeWhile) You're saying you want the first 2 results that match the given condition, whether or not they are the first ones of the initial list.

For example

var numbers = new[] {2, 3, 1, 4};
Console.WriteLine("TakeWhile");

foreach(var n in numbers.TakeWhile(x => x == 1 || x == 2))
{
    Console.Write(n);
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("WhereTake2");

foreach (var n in numbers.Where(x => x == 1 || x == 2).Take(2))
{
    Console.Write(n);
} 

Prints:

TakeWhile
2

WhereTake2
21
初见你 2024-11-02 07:37:35

我会将该子句包含在查询的 where 子句中的 TakeWhile 调用中,您有没有这样做的原因?从长远来看,两者之间可能没有太大区别,但 TakeWhile 似乎没有必要,并且会使您的代码更加混乱,IMO。

I would have included the clause in your TakeWhile call in the where clause of the query, is there a reason why you didn't do that? In the long run there probably isn't that much of a difference between the two but the TakeWhile seems unnecessary and makes your code more confusing, IMO.

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