私は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
}