Search code examples
c#linq

.ToString().ToList() returns a New List<char>


So I have a Query that searches for various items. But I just want their Id so I used a projection to return me only the Ids and not the other elements of the item. but converting from ObjectId .ToString() and then .ToList() returns me a List<char> instead List<string>

var items = await this.ItemAppService.GetAllAsync(expression,
               x => new
               {
                  Ids = x.Id.ToString().ToList(),
               });
           var Ids = items.SelectMany(x => x.Ids.Select(x => x)).ToList();

I would like to understand why i'm returning a List<Char> and how I convert it to List<String>


Solution

  • The first ToList is unnecessary, you want strings, and with that ToList() invocation you are converting your strings to char arrays. So the code should be rewritten as:

    var items = await this.ItemAppService.GetAllAsync(expression,
        x => new
        {
            Id = x.Id.ToString(),
        });
    var ids = items.Select(x => x.Id).ToList();