Ankita Soni
Ankita Soni

Reputation: 15

how i select min and max values from dataset

i want to retrive min and max value from dataset and those values use in for loop for display title in panel

    String sql = "select  title, song_id from up_song where Song_type='Mp3 Tracks' ";
    SqlDataAdapter adpt = new SqlDataAdapter(sql, cn);
    DataSet ds = new DataSet();
    adpt.Fill(ds, "title");

so here i want to find min and max value of song id... here m is max value and k min value i have no idea ...that how i select k and m...?

        for (i = m; i >= k; --i)
        {
            try
            {
                hp[i] = new HyperLink();
                hp[i].ID = "hp" + i;
                hp[i].Text = ds.Tables["title"].Rows[i].ItemArray[0].ToString();
                hp[i].NavigateUrl = "Downloadpage.aspx";
                hp[i].ForeColor = System.Drawing.Color.White;
                Panel1.Controls.Add(hp[i]);
                Panel1.Controls.Add(new LiteralControl("<br>"));
                HttpCookie coo = new HttpCookie("song");
                coo["sogtit"] = ds.Tables["title"].Rows[i].ItemArray[0].ToString();
                Response.Cookies.Add(coo);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

Upvotes: 1

Views: 5235

Answers (4)

kavitha Reddy
kavitha Reddy

Reputation: 3333

To Get MAX And MIN Values from a Dataset

   var maxTwn = mydataset.Tables[0].AsEnumerable().Max(r =>r["TWN"]);
   var minTwn = mydataset.Tables[0].AsEnumerable().Min(r =>r["TWN"]);

Upvotes: 0

gzaxx
gzaxx

Reputation: 17600

Make use of LINQ:

 var m = ds.Tables["title"].AsEnumerable().Max(x => x.Field<int>("song_id"));
 var k = ds.Tables["title"].AsEnumerable().Min(x => x.Field<int>("song_id"));

Remeber that you have to add System.Data.DataSetExtensions to have DataSet.AsEnumerable() extension :)

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460138

With using Linq extension methods:

int minSongId = ds.Tables["title"].AsEnumerable()
                  .Min(r => r.Field<int>("song_id"));
int maxSongId = ds.Tables["title"].AsEnumerable()
                  .Max(r => r.Field<int>("song_id"));

Upvotes: 1

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

What about using this?

foreach(DataRow row in ds.Tables["title"].AsEnumerable())
{
    ....
}

Upvotes: 0

Related Questions