i have a function named GetContentPageToValueVo which accepts IEnumerable<Guid> types , and here the locations and the services is of type list<string> , so how to convert this line in the argument to IEnumerable type , my locations list is coming from solr , and have valid guids in string format

Location = ScHelper.Instance().GetContentPageToValueVo(x.Locations),
                Service = ScHelper.Instance().GetContentPageToValueVo(x.Services)

2 Answers

Paul Karam On Best Solutions

You could use both the LINQ Select method and Guid.Parse to convert the List<string> to IEnumerable<Guid>.

The Select here will:

Projects each element of a sequence into a new form by incorporating the element's index.

and the Guid.Parse will:

Converts the string representation of a GUID to the equivalent Guid structure.

So your code will become:

Location = ScHelper.Instance().GetContentPageToValueVo(x.Locations.Select(Guid.Parse)),
Service = ScHelper.Instance().GetContentPageToValueVo(x.Services.Select(Guid.Parse))

In order to avoid ArgumentNullException, you could use that:

Guid y;

Location = ScHelper.Instance().GetContentPageToValueVo(x.Locations.Where(x=>!string.IsNullOrWhiteSpace(x) && Guid.TryParse(x,out y)).Select(Guid.Parse)),
Service = ScHelper.Instance().GetContentPageToValueVo(x.Services.Where(x=>!string.IsNullOrWhiteSpace(x) && Guid.TryParse(x,out y)).Select(Guid.Parse))
ehsan kiani On

Maybe this works

    Location = ScHelper.Instance().GetContentPageToValueVo(x.Locations as IEnumerable<Guid>);
if(x.Services !=null)
                    Service = ScHelper.Instance().GetContentPageToValueVo(x.Services as IEnumerable<Guid>)