简体   繁体   中英

Update a variable in list from another list using c# linq

I want to copy data from one list to another list. While copying to the new list, i want to assign new values also. Can someone please assist quickly. I want to achieve something like below,

public HashSet<string> MobileNumber { get; set; }        
var contactViewData = MobileNumber.Select(p => p).SelectMany(o => o.SplitFromCsv()).Select(x => new ContactViewMap()
{
     ContactId = Guid.NewGuid().ToString(),
     NewMobileNumber = x,
     OldMobileNumber = p
}).ToList();

public class ContactViewMap
{
    public string ContactId { get; set; }
    public string NewMobileNumber { get; set; }
    public string OldMobileNumber { get; set; }
}


public static IEnumerable<string> SplitFromCsv(this string csv)
            => csv.Split(',', StringSplitOptions.None);

Input:
{
  "MobileNumber": [
    "+9112352",
    "+9112353"
  ]
}

Select(p => p) Just returns the same item again.

I think instead of this: Select(p => p).SelectMany(o => o.SplitFromCsv())
You need this: Select(p => (p, csv: o.SplitFromCsv()) )

Now you can use that tuple in the final Select :

.Select(x => new ContactViewMap()
{
     ContactId = Guid.NewGuid().ToString(),
     NewMobileNumber = x.csv,
     OldMobileNumber = x.p
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM