使用LINQ转换此循环结构

时间:2013-01-31 10:05:00

标签: c# asp.net

请告诉我如何使用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();
    }
}

2 个答案:

答案 0 :(得分:2)

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

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

2)这里有很多条件逻辑 - 不仅仅是“根据某些条件将X设置为Y或Z”,而是“可能设置为X,但可能不是”。再说一次,这真的不适合LINQ。

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

答案 1 :(得分: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();
    }
}