我对 Linq 比较陌生。我在编码时遇到了麻烦。
我有一个包含许多不同子列表的列表。
oldList[0] some type
oldList[1] another different type
oldList[2] the type I want
oldList[3] more types
我想从特定类型中选择所有参数并将它们写入临时列表。如果该临时列表为空,我想分配一些值(值实际上并不重要)。更改值后,我想将临时列表写回 oldList。
请指教。这对我来说是一次巨大的学习经历。
public void myFunction(list)
{
//list contains at least 5 sublists of various type
//check if the type I want is null
IEnumerable<TypeIWant> possiblyEmptyList = list.OfType<TypeIWant>(); //find the type I want from the list and save it
if (possiblyEmptyList == null) //this doesn't work and possiblyEmptyList.Count <= 1 doesn't work
{
//convert residence address to forwarding address
IEnumerable<ReplacementType> replacementList = list.OfType<ReplacementType>();
forwardingAddress = replacementList.Select(x => new TypeIWant /* this statement functions exactly the way I want it to */
{
Address1 = x.Address1,
Address2 = x.Address2,
AddressType = x.AddressType,
City = x.City,
CountryId = x.CountryId,
CountyRegion = x.CountyRegion,
Email = x.Email,
ConfirmEmail = x.ConfirmEmail,
Fax = x.Fax,
Telephone = x.Telephone,
State = x.State,
PostalCode = x.PostalCode
});
//write forwarding address back to list
//don't know how to do this
}