I have some code that will loop over DataRows and will split a pipe delimited column in the DataRow into an array, and then into a List<string>.
Is there a better way to do this using LINQ? I've tried but got nowhere!
List<string> allSizes = new List<string>();
foreach (DataRow resultRow in results.Rows)
{
if (resultRow["Sizes"] != DBNull.Value)
{
string[] sizes = resultRow["Sizes"].ToString().Split('|');
foreach (string size in sizes)
{
if (!allSizes.Contains(size))
{
allSizes.Add(size);
}
}
}
}
Containson a List isn't particularly efficient. Considering that it's likely that the data set could get large, and you check whether it's in the list or not for each item, that could get expensive. It's better to use a set-based data structure to perform aDistinct, which is what you're doing, and what the LINQDistinctmethod does.