Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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 };
}
like image 241
Devendra Patidaaar Avatar asked Feb 14 '19 12:02

Devendra Patidaaar


People also ask

How do you convert a list into a set?

We can convert the list into a set using the set() command, where we have to insert the list name between the parentheses that are needed to be converted. Hence, in the above case, we have to type the set(the_names) in order to convert the names, present in the list into a set.

Can we convert list string to set?

Given a list (ArrayList or LinkedList), convert it into a set (HashSet or TreeSet) of strings in Java. We simply create an list. We traverse the given set and one by one add elements to the list.

Can we convert list to set in Java?

In this, we can convert the list items to set by using addAll() method. For this, we have to import the package java. util.

How do I convert a list to a set in Salesforce?

The simplest way to convert List to Set in Salesforce is given below: List<String> tempList = new List<String>(); Set<String> tempSet = new Set<String>(); tempList.


Video Answer


1 Answers

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};

?

like image 100
Marc Gravell Avatar answered Oct 02 '22 13:10

Marc Gravell