我有一个这样的字符串:
A00601 CALLE FCO PIETRI CALLE FRANCISCO PIETRI A20040407
有没有办法替换这段代码:
foreach (var line in parsedList)
{
if((line[0] == 'D') && (line[55] == 'Y'))
{
item.PostalCode5 = line.Substring(1, 5);
item.PreferredCityName28 = line.Substring(13,28);
...
}
}
使用这样的LINQ查询:
foreach (var line in parsedList)
{
item.PostalCode5(line.Substring(1, 5)),
item.PreferredCityName28(line.Substring(13,28))
...
.Where (line[0] == 'D' && line[55] == 'Y')
}
答案 0 :(得分:2)
parsedList
.Where(line => line[0]== 'D' && line[55] = 'Y')
.Select(itm => new Item {
PostalCode5 = line.Substring(1, 5),
PreferredCityName28 = line.Substring(13,28)
});
答案 1 :(得分:1)
你可以这样做:
parsedList.Where(line => line[0]== 'D' && line[55] = 'Y')
.ToList()
.ForEach(line => {
item.PostalCode5(line.Substring(1, 5)),
item.PreferredCityName28(line.Substring(13,28))
});
与您的代码相同。