views:

144

answers:

1

Consider the following snippet of code:

// get number of sheep in DataTable by counting UID's
Double n = DataTableContainingSheep.AsEnumerable().Sum(r => (Int32)r["sheepId"])

What I if want to count only the black sheep in the DataTable? Is there any way I can fit a select-clause into the Sum() function?

+1  A: 

It should be something like:

Double n =
    DataTableContainingSheep
        .AsEnumerable()
        .Where(r => (String)r["color"] == "black")
        .Sum(r => (Double)r["sheepId"]);
Bruno Reis
Thanks Bruno :)
roosteronacid
The DataTable is pure fiction. I just invented some example-code to help me explain what I wanted to achieve.
roosteronacid
Cool! Glad it was useful for you.
Bruno Reis