# Get Max value for selected column of each row in Linq [Answered]RSS

## 5 replies

Last post Oct 27, 2015 11:50 PM by neerajkumarmodi

Member

33 Points

118 Posts

### Get Max value for selected column of each row in Linq

Hi ,

I am trying to calculate Min and Max Values for Particular Columns for all rows in linq for example

 id datetime name a b c Max_value Min_value 1 27-12-15 Alok 2 4.3 7 7 2

Trying to get the max and min value with below code but it takes the first 3 column in sequence,Is there a way to calculate min and max for particular column as shown above?

var query = from std in finaldt.AsEnumerable()
select new
{
Time = Convert.ToDateTime(std["datetime"]),
Name = Convert.ToString(std["name"]),
Max_value = (from o in std.ItemArray.Take(3) where o != DBNull.Value select (o)).Max()

};

NWRITER

Star

8490 Points

1446 Posts

### Re: Get Max value for selected column of each row in Linq

Oct 27, 2015 03:12 AM|Klein Zhang|LINK

Hi neerajkumarmodi,

#### neerajkumarmodi

I am trying to calculate Min and Max Values for Particular Columns for all rows in linq for example

If you'd like to calculate Min and Max Values for Particular Columns for all rows, you could try with the code below to get the row.

```MinRow:           var result = dt.AsEnumerable().OrderBy(x => x["a"]).First();
MaxRow:            var result1 = dt.AsEnumerable().OrderByDescending(x => x["a"]).First();```

I hope it's helpful to you.

Best Regards,

Klein zhang

Member

33 Points

118 Posts

### Re: Get Max value for selected column of each row in Linq

Hi,

I need to calculate at row level as below

 id datetime name a b c Max_value Min_value 1 27-12-15 Alok 2 4.3 7 7 2
NWRITER

Member

33 Points

118 Posts

### Re: Get Max value for selected column of each row in Linq

Hi ,

Max_value = (from o in std.ItemArray.Take(3) where o != DBNull.Value select (o)).Max()

above gives me max value but it select first 3 column by default..is there a way to select any selected column

NWRITER

Star

8490 Points

1446 Posts

### Re: Get Max value for selected column of each row in Linq

Oct 27, 2015 10:20 PM|Klein Zhang|LINK

Hi neerajkumarmodi,

#### neerajkumarmodi

above gives me max value but it select first 3 column by default..is there a way to select any selected column

In my experience, maybe you couldn't  get the columns of the discontinuous using linq directly. But you could get the continuous column.

For example , when you want to get the column 4,5,6.

```   var query1 = from std in dt.AsEnumerable()
select new
{
Time = Convert.ToDateTime(std["datetime"]),
Name = Convert.ToString(std["name"]),
Max_value = (from o in std.ItemArray.Skip(3). where o != DBNull.Value select (o)).Max()

};```

I hope it's helpful to you.

Best Regards,

Klein zhang

Member

33 Points

118 Posts

### Re: Get Max value for selected column of each row in Linq

Thanks klein!

You are correct we can skip and then take from arraylist.

`Max_value = (from o in std.ItemArray.Skip(3).Take(3). where o != DBNull.Value select (o)).Max()`
NWRITER