开发者

Converting IFs to LINQ

Could someone help me converting this to LINQ? Any help/tips appreciated...

IList<DestinationSummary> list =
    _invoiceReportDao.InvoiceDestinationSummary(filter);

IList<DestinationSummary> updatedList =
    new List<DestinationSummary>();

foreach (DestinationSummary item in list)
{
    if (item.ChargeCategoryDiscriminator == "INT")
        item.ChargeCategoryDiscriminator = "00%";

    if (item.ChargeCategoryDiscriminator == "UK")
        item.ChargeCategoryDiscriminator = "07%";

    updatedList.Add(ite开发者_StackOverflowm);
}

return updatedList;


LINQ should not be used to modify objects.

Your code is fine as-is.
However, you can make it a little bit faster by initializing the new list form the old one:

IList<DestinationSummary> list = _invoiceReportDao.InvoiceDestinationSummary(filter);
IList<DestinationSummary> updatedList = new List<DestinationSummary>(list);

foreach (DestinationSummary item in updatedList)
{
    if (item.ChargeCategoryDiscriminator == "INT")
        item.ChargeCategoryDiscriminator = "00%";

    if (item.ChargeCategoryDiscriminator == "UK")
        item.ChargeCategoryDiscriminator = "07%";
}


list.Where(i => i.ChargeCategoryDiscriminator == "INT")
    .ForEach(i => i.ChargeCategoryDiscriminator ="00%");
list.Where(i => i.ChargeCategoryDiscriminator == "UK")
    .ForEach(i => i.ChargeCategoryDiscriminator ="07%");

Also, adding these items to updatedList is redundant. The changes are made to list.


Creating a new list is redundant. You are iterating over the list, modifying each item, and then adding the modified items to the new list. The new list is thus identical to the old list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜