Home > Enterprise >  comma separated values of specific column in table to list of integers in LINQ
comma separated values of specific column in table to list of integers in LINQ

Time:07-12

I have one column with values 12,13,14 and I want that to be comma split and stored in list of integers List<int>. How can I do that?

 List<int> list1 = new List<int>();
 list1 = Array.ConvertAll(
     (
         from rar in unitOfWork.StudentRepository.GetAsQueryable()
         where rar.RequesterId == userId && rar.StatusId == 0
         select new { rar.RoleIds }
     )
 .AsEnumerable()
 .Select(r => string.Join(",", r.RoleIds))
 .ToArray(), int.Parse).ToList();

CodePudding user response:

string.Split and SelectMany are probably the pieces you're missing.

List<int> list1 =
     (
         from rar in unitOfWork.StudentRepository.GetAsQueryable()
         where rar.RequesterId == userId && rar.StatusId == 0
         select new { rar.RoleIds }
     )
    .AsEnumerable()
    .SelectMany(r => r.RoleIds.Split(','))
    .Select(int.Parse)
    .ToList();
  • Related