 We are excited to announce that the ASP.NET Forums are moving to the new Microsoft Q&A experience. Learn more >

# sum values in datatable using linq based on conditions[Answered] RSS

## 10 replies

Last post Apr 23, 2015 08:03 AM by Lokesh B R

• ### sum values in datatable using linq based on conditions

Apr 23, 2015 03:59 AM|vignesht5|LINK

Hi,

I'm having a datatable like mentioned below.

```ID   Percentage
1        50
1        30
2        0
2        100  ```

Result:

```ID   Percentage
1        80
2       100```

I tried this and it doesn't work

```var n =  dt.AsEnumerable().Where(r => (int)r["ID"] != "0" || (int)r["ID"] != "100").
Sum(r => (int)r["Percentage"]);```

Now I need to sum the percentage for each ID and the percentage for each ID should be 0 or 100 percentage.

If any one of the ID in table doesn't have 0 or 100 I need to alert. Pls suggest me how I can do this in linq and I think its the best way.

Linq asp.net

• ### Re: sum values in datatable using linq based on conditions

Apr 23, 2015 04:10 AM|Lokesh B R|LINK

Hi,

Check this query.

```DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Percentage", typeof(int));

table.Rows.Add(1, 50);
table.Rows.Add(1, 30);
table.Rows.Add(2, 0);
table.Rows.Add(2, 100);

var result = (from p in table.AsEnumerable()
group p by p["ID"]
into r
select new
{
ID = r.Key,
Percentage = r.Sum((s) => decimal.Parse(s["Percentage"].ToString()))
}).ToList();```

Linq asp.net

Mark the post as answer, if it helps you.
• ### Re: sum values in datatable using linq based on conditions

Apr 23, 2015 04:14 AM|vignesht5|LINK

#### Lokesh B R

Hi,

Check this query.

```DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Percentage", typeof(int));

table.Rows.Add(1, 50);
table.Rows.Add(1, 30);
table.Rows.Add(2, 0);
table.Rows.Add(2, 100);

var result = (from p in table.AsEnumerable()
group p by p["ID"]
into r
select new
{
ID = r.Key,
Percentage = r.Sum((s) => decimal.Parse(s["Percentage"].ToString()))
}).ToList();```

Thanks for the prompt reply. How can I alert if the conditions get satisfied.

Linq asp.net

• ### Re: sum values in datatable using linq based on conditions

Apr 23, 2015 04:15 AM|Lokesh B R|LINK

Hi,

Can you post some sample data for that?

i didn't get your point here.

Linq asp.net

Mark the post as answer, if it helps you.
• ### Re: sum values in datatable using linq based on conditions

Apr 23, 2015 04:22 AM|jed0228|LINK

```foreach (var item in result)
{
if (item.Percentage != 0 || item.Percentage != 100)
MessageBox.Show("Your alert here");
}```

Try adding this after Lokesh's answer for the alert.

Linq asp.net

• ### Re: sum values in datatable using linq based on conditions

Apr 23, 2015 05:10 AM|vignesht5|LINK

#### Lokesh B R

Hi,

Can you post some sample data for that?

i didn't get your point here.

The below is the result we get after checking the condition.

If sum of any of the ID doesn't have 0 or 100 then I need to alert. Also can ID be considered as string.

```ID   Percentage
1        80
2       100```

Linq asp.net

• ### Re: sum values in datatable using linq based on conditions

Apr 23, 2015 05:27 AM|Lokesh B R|LINK

try this

``` DataTable table = new DataTable();
table.Columns.Add("ID", typeof(string));
table.Columns.Add("Percentage", typeof(int));

table.Rows.Add("1", 50);
table.Rows.Add("1", 30);
table.Rows.Add("2", 0);
table.Rows.Add("2", 100);

var result = (from p in table.AsEnumerable()
group p by p["ID"]
into r
select new
{
ID = r.Key,
Percentage = r.Sum((s) => decimal.Parse(s["Percentage"].ToString()))
}).ToList();

foreach (var item in result)
{
if (item.Percentage != 0 || item.Percentage == 100)
{
string message = item.ID + " percentage is " + item.Percentage;
// Show the message in ALERT
}
}```

Linq asp.net

Mark the post as answer, if it helps you.
• ### Re: sum values in datatable using linq based on conditions

Apr 23, 2015 05:37 AM|vignesht5|LINK

#### Lokesh B R

try this

``` DataTable table = new DataTable();
table.Columns.Add("ID", typeof(string));
table.Columns.Add("Percentage", typeof(int));

table.Rows.Add("1", 50);
table.Rows.Add("1", 30);
table.Rows.Add("2", 0);
table.Rows.Add("2", 100);

var result = (from p in table.AsEnumerable()
group p by p["ID"]
into r
select new
{
ID = r.Key,
Percentage = r.Sum((s) => decimal.Parse(s["Percentage"].ToString()))
}).ToList();

foreach (var item in result)
{
if (item.Percentage != 0 || item.Percentage == 100)
{
string message = item.ID + " percentage is " + item.Percentage;
// Show the message in ALERT
}
}```

I think its close. Can I have percentage as int?

Linq asp.net

• ### Re: sum values in datatable using linq based on conditions

Apr 23, 2015 05:52 AM|Lokesh B R|LINK

Hi,

#### vignesht5

Can I have percentage as int?

It's already INT.

```            table.Columns.Add("Percentage", typeof(int));
```

Linq asp.net

Mark the post as answer, if it helps you.
• ### Re: sum values in datatable using linq based on conditions

Apr 23, 2015 05:57 AM|vignesht5|LINK

Hi,

#### vignesht5

Can I have percentage as int?

It's already INT.

```            table.Columns.Add("Percentage", typeof(int));
```

In this part I need to have percentage as int.

``` select new
{
ID = r.Key,
Percentage = r.Sum((s) => decimal.Parse(s["Percentage"].ToString()))
}).ToList();```

Linq asp.net

• ### Re: sum values in datatable using linq based on conditions

Apr 23, 2015 08:03 AM|Lokesh B R|LINK

Hi,

try this

```var result = (from p in table.AsEnumerable()
group p by p["ID"]
into r
select new
{
ID = r.Key,
Percentage = r.Sum((s) => int.Parse(s["Percentage"].ToString()))
}).ToList();```

Linq asp.net

Mark the post as answer, if it helps you.