locked
@Foreach how to select top 1 RRS feed

  • Question

  • User982203039 posted

    I am getting data from a Stored Procedure. I am update a table for Birthdays for the next 7 days. My question in in my view I am wanting to display a header from the Database filed (BDate) of the date of the birthday. My question is how can I use the @Foreach and select top 1 to only display the date 1 time?

    Controller:

    public ActionResult CustomerBirthdays()
    {
    	var Div = @Session["Div"];
    	List<Birthdays> birthdaylist = new List<Birthdays>();
    	string CS = ConfigurationManager.ConnectionStrings["yyy"].ConnectionString;
    	using (SqlConnection con = new SqlConnection(CS))
    	{
    		SqlCommand cmd = new SqlCommand("GetBirthdaySP", con);
    		cmd.CommandType = CommandType.StoredProcedure;
    		cmd.Parameters.Add("@Div", SqlDbType.VarChar).Value = Div;
    		con.Open();
    		SqlDataReader rdr = cmd.ExecuteReader();
    		while (rdr.Read())
    		{
    			Birthdays bd = new Birthdays();
    			bd.ID = Convert.ToInt32(rdr["ID"]);
    			bd.DivName = rdr["DivName"].ToString();
    			bd.BDate = rdr["Bdate"].ToString();
    			birthdaylist.Add(bd);
    		}
    	}
    	return View(birthdaylist);

    View:

    <tr>
    	@foreach (Birthdays bd in Model)
    	{
    		<th style="width:900px">@bd.BDate</th>
    	}
    </tr>

    Friday, August 7, 2020 9:41 PM

All replies

  • User303363814 posted
    <tr>
       <th style="width:900px">@bd.First().BDate</th>
    </tr>

    Saturday, August 8, 2020 10:23 AM
  • User1686398519 posted

    Hi Baze72,

    You can use the Take() method to extract the first n elements from the beginning of the target sequence (where n is the method parameter).

    @foreach (Birthdays bd in Model.Take(1))
    {
        <th style="width:900px">@bd.BDate</th>
    }

    Best Regards,

    YihuiSun

    Monday, August 10, 2020 6:16 AM
  • User982203039 posted

    <tr>
       <th style="width:900px">@bd.First().BDate</th>
    </tr>

    I get this:

     CS1061: 'Birthdays' does not contain a definition for 'First' and no extension method 'First' accepting a first argument of type 'Birthdays' could be found (are you missing a using directive or an assembly reference?)

    Monday, August 10, 2020 1:06 PM
  • User982203039 posted

    YihuiSun

    Hi Baze72,

    You can use the Take() method to extract the first n elements from the beginning of the target sequence (where n is the method parameter).

    @foreach (Birthdays bd in Model.Take(1))
    {
        <th style="width:900px">@bd.BDate</th>
    }

    Best Regards,

    YihuiSun

    I get :

    System.Collections.Generic.List<PBTIntranet.Models.Birthdays>' does not contain a definition for 'Take'

    Here is my controller, this might help:

    {
    	SqlCommand cmd = new SqlCommand("BirthdaySP", con);
    	cmd.CommandType = CommandType.StoredProcedure;
    	cmd.Parameters.Add("@Div", SqlDbType.VarChar).Value = div;
    	con.Open();
    	SqlDataReader rdr = cmd.ExecuteReader();
    	while (rdr.Read())
    	{
    		Birthdays bd = new Birthdays();
    		bd.ID = Convert.ToInt32(rdr["ID"]);
    		bd.Customer = rdr["Customer"].ToString();
    		bd.Day = Convert.ToInt32(rdr["Day"]);
    		bd.BDate = rdr["Bdate"].ToString();
    		birthdaylist.Add(bd);
    }

    Monday, August 10, 2020 1:11 PM
  • User-183185495 posted

    You should really be doing this in the sql using the Top command think how long it will take to process during a for loop with that added burden

    SELECT TOP number|percent column_name(s)
    FROM table_name
    WHERE condition;

    https://www.w3schools.com/sql/sql_top.asp

    I am getting data from a Stored Procedure. I am update a table for Birthdays for the next 7 days. My question in in my view I am wanting to display a header from the Database filed (BDate) of the date of the birthday. My question is how can I use the @Foreach and select top 1 to only display the date 1 time?

    Controller:

    public ActionResult CustomerBirthdays()
    {
    	var Div = @Session["Div"];
    	List<Birthdays> birthdaylist = new List<Birthdays>();
    	string CS = ConfigurationManager.ConnectionStrings["yyy"].ConnectionString;
    	using (SqlConnection con = new SqlConnection(CS))
    	{
    		SqlCommand cmd = new SqlCommand("GetBirthdaySP", con);
    		cmd.CommandType = CommandType.StoredProcedure;
    		cmd.Parameters.Add("@Div", SqlDbType.VarChar).Value = Div;
    		con.Open();
    		SqlDataReader rdr = cmd.ExecuteReader();
    		while (rdr.Read())
    		{
    			Birthdays bd = new Birthdays();
    			bd.ID = Convert.ToInt32(rdr["ID"]);
    			bd.DivName = rdr["DivName"].ToString();
    			bd.BDate = rdr["Bdate"].ToString();
    			birthdaylist.Add(bd);
    		}
    	}
    	return View(birthdaylist);

    View:

    <tr>
    	@foreach (Birthdays bd in Model)
    	{
    		<th style="width:900px">@bd.BDate</th>
    	}
    </tr>

    Monday, August 10, 2020 1:22 PM
  • User1686398519 posted

    Hi Baze72,

    Did you get this error when using Take() on the view?I did not reproduce your problem.You can refer to the complete example I gave below.

    • Only the modified code is given in the example.

    Note: If you use Take() in the controller and this error occurs, you need to check whether you add "using System.Linq;"

    View

    @model IEnumerable<WebApplication26.Models.Birthdays>
    @using WebApplication26.Models
    <table>
         <tr>
            @foreach (Birthdays bd in Model.Take(1))
             {
               <th style="width:900px">@bd.BDate</th>
             }
         </tr>
    </table>

    Here is the result.

    Best Regards,

    YihuiSun

    Tuesday, August 11, 2020 9:51 AM