Question: The following linq

```
var subjectMarks = (from DataRow row in objDatatable.Rows
                    select Convert.ToDecimal(row["EXM_MARKS"])).Sum();

```

throws an exception since some `row["EXM_MARKS"]` has non numeric values like `AB` etc.
How can I get the sum of only numeric ones out of them?

Answer: 
You could create an extension method `SafeConvertToDecimal` and use this in your LINQ query:

```
var subjectMarks = (from DataRow row in objDatatable.Rows
                    select row["EXM_MARKS"].SafeConvertToDecimal()).Sum();

```

This extension method would look like this:

```
public static decimal SafeConvertToDecimal(this object obj)
{
    decimal result;
    if(!decimal.TryParse(obj.ToString(), out result))
        result = 0;

    return result;
}

```

This approach has the advantage that it looks clean and you aren't converting each object twice as do all other answers.
You can make some extensions utility class like this for elegant solution:

```
public static class TypeExtensions
{
    public static bool IsValidDecimal(this string s)
    {
        decimal result;
        return Decimal.TryParse(s, out result);
    }
}

```

and use it in this way:

```
var subjectMarks = (from DataRow row in objDatatable.Rows
                     where row["EXM_MARKS"].IsValidDecimal()
                     select Convert.ToDecimal(row["EXM_MARKS"])).Sum();

```

Hope this helps.