Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert list of strings to list of guids

Tags:

c#

I have following line of code which creates an list of strings.

List<string> tstIdss = model.Ids.Where(x => x.Contains(entityId)).Select(x => x.Split('_').First()).ToList();

I need to convert it into list of Guids. i.e. List<Guid> PermissionIds.

model.PermissionIds= Array.ConvertAll(tstIdss , x => Guid.Parse(x));

I tried the above way but getting the following error. model.PermissionIds is implemented as following in my model class.

public List<Guid> PermissionIds { get; set; }

Error 3

>>The type arguments for method 'System.Array.ConvertAll<TInput,TOutput>(TInput[], System.Converter<TInput,TOutput>)' 
    cannot be inferred from the usage. 
    Try specifying the type arguments explicitly.   
like image 359
immirza Avatar asked Jun 28 '15 16:06

immirza


People also ask

How to convert string List to Guid List in c#?

ConvertAll(tstIdss , x => Guid. Parse(x));

How do I convert a list of strings to a list of objects?

Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> . List<Object> objectList = new ArrayList<Object>(stringList);


1 Answers

You can use Linq's Select and ToList methods:

model.PermissionIds = tstIdss.Select(Guid.Parse).ToList();

Or you can use the List<T>.ConvertAll method:

model.PermissionIds = tstIdss.ConvertAll(Guid.Parse);
like image 120
Thomas Levesque Avatar answered Oct 19 '22 03:10

Thomas Levesque