去 .Take() 还是不去 .Take(),这就是问题
我有一个代表每个年龄段的人口(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()
问题
- 如果我在
dr.Year 上使用
.Take(2)
以及补充where
条件,效果会更好吗? ? - 还有更好的选择吗?
- 我的方法值得考虑吗?
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
- Would it have been best if I had used
.Take(2)
along with a supplementalwhere
condition on thedr.Year
? - Are there any better alternatives?
- Is my approach worth considerations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上#1 可能会产生与当前代码不同的结果。例如,如果顺序是随机的(我在任何地方都看不到任何排序),当前代码可能会返回任意数量的结果(例如,如果第一个元素不匹配任一条件,则没有结果,即使后续元素匹配)
听起来像方法 #1 会更正确(除非您实际上想要
TakeWhile
的行为)您是说您想要匹配给定条件的前 2 个结果,无论它们是否是第一个结果初始清单。例如
打印:
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
Prints:
我会将该子句包含在查询的
where
子句中的TakeWhile
调用中,您有没有这样做的原因?从长远来看,两者之间可能没有太大区别,但TakeWhile
似乎没有必要,并且会使您的代码更加混乱,IMO。I would have included the clause in your
TakeWhile
call in thewhere
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 theTakeWhile
seems unnecessary and makes your code more confusing, IMO.