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.
精彩评论