-5

你能告诉我如何使用 LINQ 转换这个循环结构吗?

list<Entity> auditlist=retreivedata()\\method to fetch data
foreach (Entity obj in auditList)

    obj.CultSpecificRevisedData = "NULL";
    obj.CultSpecificPublishedData = "NULL";

    if (obj.RevisedData != null && obj.RevisedData != "NULL")
        obj.CultSpecificRevisedData = ConvertToProfileSpecificFormat(Convert.ToDecimal(obj.RevisedData), DecimalSeparator);
    if (obj.PublishedData != null && obj.PublishedData != "NULL")
        obj.CultSpecificPublishedData = ConvertToProfileSpecificFormat(Convert.ToDecimal(obj.PublishedData), DecimalSeparator);

    var yearPart = obj.CalendarYear;
    var monthPart = string.Empty;
    var frequencyName = GetEnglishFrequencyBame(frequencyTypeMasId);
    if (frequencyName == FrequencyType.Monthly)
    {
        monthPart = new DateTime(obj.CalendarYear, GetMonthNumber(obj.Month), 1).ToString("MMM");
        obj.CultSpecificPeriod = monthPart + "-" + yearPart.ToString();
    }
    if (frequencyName == FrequencyType.Quarterly)
    {
      UserMessage = obj.QuarterName;
        obj.CultSpecificPeriod = UserMessage + "-" + yearPart.ToString();
    }
    else if (frequencyName == FrequencyType.BiAnnually)
    {
        UserMessage = obj.SemesterName;
        obj.CultSpecificPeriod = UserMessage + "-" + yearPart.ToString();
    }
    else
    {
        obj.CultSpecificPeriod = yearPart.ToString();
    }
}
4

2 回答 2

2

我建议您不要尝试将其更改为使用 LINQ:

1)它正在修改对象。这不是 LINQ 的设计目的。它是为查询而设计的。

2)这里有很多条件逻辑 - 不仅仅是“根据某些条件将 X 设置为 Y 或 Z”,而是“可能设置 X,但可能不设置”。同样,这确实不适合 LINQ。

建议尝试将一些功能提取到小方法中 - 处理频率名称的部分当然可以更清晰地表达,很可能使用开关,并提取通用功能 - 但这不是转换为 LINQ 的问题,一定。

于 2013-01-31T10:08:21.333 回答
0

对于它的价值,您可能希望代码更像这样。我希望这在某种程度上有所帮助。

foreach (var e in retreivedata())
{
    e.CultSpecificRevisedData = null;
    e.CultSpecificPublishedData = null;

    if (!string.IsNullOrWhiteSpace(e.RevisedData))
    {
        e.CultSpecificRevisedData = ConvertToProfileSpecificFormat(
            Convert.ToDecimal(e.RevisedData),
            DecimalSeparator);
    }

    if (!string.IsNullOrWhitespace(e.PublishedData))
    {
        e.CultSpecificPublishedData = ConvertToProfileSpecificFormat(
            Convert.ToDecimal(e.PublishedData),
            DecimalSeparator);
    }

    switch (GetEnglishFrequencyBame(frequencyTypeMasId))
    {
        case FrequencyType.Monthly:
            var f = CultureInfo.CurrentCulture.DateTimeFormat;
            obj.CultSpecificPeriod = string.Format(
                "{0}-{1}",
                f.GetAbbreviatedMonthName(GetMonthNumber(e.Month)),
                e.CalendarYear);
            break;

        case FrequencyType.Quarterly:            
            UserMessage = e.QuarterName;
            e.CultSpecificPeriod = string.Format(
                "{0}-{1}",
                e.QuarterName,
                e.CalendarYear)
            break;

        case FrequencyType.BiAnnually:
            UserMessage = e.SemesterName;
            e.CultSpecificPeriod = string.Format(
                "{0}-{1}",
                e.SemesterName,
                e.CalendarYear)
            break;

        default:
            obj.CultSpecificPeriod = e.CalendarYear.ToString();
    }
}
于 2013-01-31T10:48:24.527 回答