简体   繁体   中英

How to convert List<long?> to ISet<long>

There is a methods which is accepting a parameter of type List<long?> , I need to assign it to someTestModel ids, which are of type ISet<long> .

public void testM1(List<long?> testIds)
{
    var request = new someTestModel { ids= testIds };
}

There's two things we'd need here:

  • a concrete type to implement ISet<T> - presumably HashSet<T> will suffice
  • to change from long? to long - presumably by just ignoring any that are null

So, something like:

var hash = new HashSet<long>();
foreach(var id in testIds) {
    if(id.HasValue) hash.Add(id.Value);
}
var request = new someTestModel{ ids = hash};

?

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