Is there anyway to combine the 2 linq expressions into one? I.e. so one LINQ expression will return both the DistCount and the NormCount into the 2 separate int variables.
DistCount = (from string row in myList[i]
                where row.Length > 0
                select row).Distinct().Count();
NormCount = (from string row in myList[i]
                where row.Length > 0
                select row).Count();
                do a group by row.  You'll then have the distinct count (# of groups) and the total (sum of Counts)
var q = (from string row in myList[i]
    where row.Length > 0
    group row by row into rowCount
    select new {rowCount.Key, rowCount.Count})
int distinct = q.Count();
int total = q.Sum(r=>r.Count);
                        To answer your question. There's no built-in linq expression for that.
Side note. If you really need it you can create one.
public static class Extensions
{
    public static Tuple<int, int> DistinctAndCount<T>(this IEnumerable<T> elements)
    {
        HashSet<T> hashSet = new HashSet<T>();
        int count = 0;
        foreach (var element in elements)
        {
            count++;
            hashSet.Add(element);
        }
        return new Tuple<int, int>(hashSet.Count, count);
    }
}
You can create your named return type instead of Tuple to make the usage easier.
Example usage will look like:
   var distinctAndCount = (from string row in myList[i]
                              where row.Length > 0 
                              select row
                             ).DistinctAndCount();
Or as I personally would prefer to write it:
   var distinctAndCount = myList[i].Where(row => row.Length > 0).DistinctAndCount();
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With