0

我想指定首次产生成本的时间以及以年为单位的重复周期,这意味着成本会一次又一次地产生。所以我创建了一个如下所示的成本模型:

public class Cost
{
    public Cost()
    {
        Year = 1;
    }

    public decimal Amount { get; set; }

    public int AnsweredQuestionID { get; set;}

    public virtual AnsweredQuestion AnsweredQuestion {get; set;}

    public int? RepeatPeriod { get; set; }
}

现在我想返回两个日期之间产生的费用,最好使用 Linq。

编辑我过度简化了我的问题。我有在特定日期和一段时间后再次发生的 PropertyCosts。首次发生成本的日期是从物业被调查之日起计算的。成本模型存储重复周期并与特定问题/答案相关。如果某个问题以特定方式回答了某项财产,则会产生费用。所以我的代码看起来有点像这样(仍然试图在这里简化)但目前我只得到第一次出现

    public IEnumerable<PropertyCost> GetCosts(DateTime startDate, DateTime endDate)
    {
       IQueryable<AnsweredQuestion> answeredQuestions = 
                           _Uow.AnsweredQuestionRepository
                               .All
                               .Where(x => x.PropertySurvey.PropertyID == id);
       IQueryable<Cost> allCosts = UOW.CostRepository.All;

       IQueryable<PropertyCost> firstOccurences = from aq in answeredQuestions
              from c in costs
              where aq.ID == c.AnsweredQuestionID
              select new PropertyCost
              {
                  QuestionText = aq.Question.Text,
                  AnswerText = aq.Answer.Text,
                  UnitCost = c.Amount,
                  Date = aq.PropertySurvey.Survey.StartDate,
                  RepeatYears = c.RepeatPeriod
              });
       //but now I need to insert PropertyCosts for recurring costs that occur when RepeatPeriod is not null

    }
4

2 回答 2

1

这个怎么样....

var costs = firstOccurences.SelectMany (p => 
           from x in Enumerable.Range(0, (p.RepeatYears ?? 0) > 0
                               ? ((endDate.Year - p.Date.Year)+1)/p.RepeatYears.Value
                               : 1)
        let date = p.Date.AddYears(x * p.RepeatYears??0)
        where startDate <= date && endDate >= date
        select new PropertyCost {
          QuestionText=p.QuestionText,
          AnswerText=p.AnswerText,
          UnitCost = p.UnitCost,
          Date = date,
          RepeatYears = p.RepeatYears
        }
    )

由于翻译功能,您可能需要先将 firstOccurences 转换为 Enumerable

例如

          IEnumerable<PropertyCost> firstOccurences = (from aq in answeredQuestions
          from c in costs
          where aq.ID == c.AnsweredQuestionID
          select new PropertyCost
          {
              QuestionText = aq.Question.Text,
              AnswerText = aq.Answer.Text,
              UnitCost = c.Amount,
              Date = aq.PropertySurvey.Survey.StartDate,
              RepeatYears = c.RepeatPeriod
          }).AsEnumerable();

这是一个快速修复,可以用计算替换初始0Enumerable.Range

于 2013-08-23T15:58:44.377 回答
1

首先,我不知道您为什么要退回收藏Cost品。您应该创建另一个class来处理该数据,例如:

public class CostOccurrence
{
    public decimal Amount { get; set; }
    public DateTime Occurrence { get; set; }
}

然后实现你的方法:

public IEnumerable<CostOccurrence> GetCosts(DateTime startDate, DateTime endDate)
{
    DateTime current = FirstIncurred;

    while (current < startDate)
        current = current.AddYears(RepeatPeriod);

    while (current >= startDate && current < endDate)
    {
        yield return new CostOccurrence { Amount = Amount, Occurrence = current };
        current = current.AddYears(RepeatPeriod);
    }
}
于 2013-08-20T09:34:16.650 回答