Sunday, December 6, 2020

Assorted LINQ Solutions

Here I am trying to bring all problems which are commonly faced by developers and look for help for solutions. These are the scenarios which I faced at different times and got solution on my own or somewhere on google. I will keep updating it as I recollect it from my experience.

1) Convert all the comma separated numbers in a string to list of int.
    e.g. string numbers = "1,2,3";

Covert above numbers to list of int

var list = Array.ConvertAll<string, int>(numbers.Split(','), Convert.ToInt32);

Above solution works well till the time it is just comma separated numbers. If you add trailing comma or any other character in this string, it will throw exception.
For example: 
string numbers = "1,2,3,"; 
            OR
string numbers = "1,a,3";