所以我有这个代表:
private static readonly Func<GetAddress, GetNames> ConvertAddress = a => new Address() {
StreetNumber = a.streetNumber,
City = a.city,
State = a.state,
zip = a.zip,
firstName = a.firstName,
LastName = a.lastName
};
我称之为:
ADRESS = o.Addresses.Select(ConvertAddress).First(), ...
所以我想要一些名字和姓氏的过滤器。如果它们被使用了一次,我想在下次调用它时使用null。不是地址为null,而只是名称。
所以,如果john johnson经历了这两次,我想第二次john johnson通过这个将他的名字分配给“”或null。
答案 0 :(得分:0)
显然,您需要在每个呼叫实例之间共享某种状态。最简洁的方法是将其传递给你,这样你的函数就会改变为:
private static readonly Func<GetAddress, ISet<string>, GetNames> ConvertAddress = a, usedNames =>
{
var fullName = string.Format("{0} {1}", a.FirstName, a.LastName);
var fullNameHasBeenUsed = usedNames.Contains(fullName);
usedNames.Add(fullName);
return new Address() {
StreetNumber = a.streetNumber,
City = a.city,
State = a.state,
zip = a.zip,
firstName = fullNameHasBeenUsed ? null : a.firstName,
LastName = fullNameHasBeenUsed ? null : a.lastName
};}
然后你会这样称呼:
var usedFirstNames = new HashSet<string>();
ADRESS = o.Addresses.Select(a => ConvertAddress(a, usedFirstNames)).First()