Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
here is asp code
ASP.NET
<div class="form-group">
           <label class="col-sm-2 control-label">
                   Select Sector:
               </label>
               <div class="col-sm-3">
                  <asp:DropDownList CssClass="form-control" runat="server" MaxLength="7" ID="ddlToSector" OnSelectedIndexChanged="ddlToSector_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
                    </div>
           <label class="col-sm-2 control-label">
               Strenght:
           </label>
           <div class="col-sm-1">
               <asp:TextBox runat="server" ID="txtStrenght" AutoPostBack="true" ></asp:TextBox>
           </div>
       </div>

       <div class="form-group">
           <label class="col-sm-2 control-label">
               OnLeave:
           </label>
           <div class="col-sm-3">
               <asp:TextBox runat="server" ID="txtOnLeave" AutoPostBack="true" ></asp:TextBox>
           </div>
           <label class="col-sm-2 control-label">
               Absent:
           </label>
           <div class="col-sm-3">
               <asp:TextBox runat="server" ID="txtAbsent" AutoPostBack="true"></asp:TextBox>
           </div>
       </div>
       <div class="form-group">
           <label class="col-sm-2 control-label">
               Special Duty:
           </label>
           <div class="col-sm-3">
               <asp:TextBox runat="server" ID="txtSpecialDuty" AutoPostBack="true" ></asp:TextBox>
           </div>
           <label class="col-sm-2 control-label">
               On Course:
           </label>
           <div class="col-sm-3">
               <asp:TextBox runat="server" ID="txtOnCourse" AutoPostBack="true" ></asp:TextBox>
           </div>
       </div>

C# CODE

C#
protected void ddlToSector_SelectedIndexChanged(object sender, EventArgs e)
       {

           var sectorStrength = _service.GetAllEmployeeDuty().Where(x => x.ToSector_Id == SafeConvert.ToInt32(ddlToSector.SelectedValue)).Count();
               txtStrenght.Text = sectorStrength.ToString();
               var sectorLeave = _service.GetAllEmployeeStatus().Where(x => x.EmpstatusType_Id  == SafeConvert.ToInt32(ddlToSector.SelectedValue)).Count();
               txtOnLeave.Text = sectorLeave.ToString();
               var sectorAbsent = _service.GetAllEmployeeAbsent().Where(x => x.Sector_Id ==SafeConvert.ToInt32(ddlToSector.SelectedValue)).Count();
               txtAbsent.Text = sectorAbsent.ToString();


          }
       }


I want to get total no. of employee with same EmpStatus_Id and show then in respective text box.
from above code i got total no employee in the table against on selecting sector from the dropdown.
code flow in the way that when selecting sector from the drop down.we get total employee of that sector then no of absent,on leave,medical etc in the text box.
Posted
Updated 18-Nov-14 19:15pm
v2

I'd suggest to download it: 101 LINQ Samples in C#[^]. There you'll find what you're looking for ;)

For further information, please see:
How to: Write LINQ Queries in C#[^]
How to: Count, Sum, or Average Data by Using LINQ (Visual Basic)[^] - this is what you need ;)
Aggregate Queries[^]

I'm using LinqPad[^] quite often. It might help you to write proper query.

Example:
C#
void Main()
{
	List<Human> humans = new List<Human> {
			new Human("Andrew", 25),
			new Human("Marta", 24),
			new Human("Roberto", 25),
			new Human("Zenobia", 21),
			new Human("Utopia", 25),
			new Human("Ramdion", 20),
			new Human("Ewa", 25),
			new Human("Jeremy", 24),
			new Human("Isabell", 25),
			new Human("Isaura", 23),
			new Human("Gregor", 25),
			new Human("Evan", 23),
			new Human("Natasha", 25),
			new Human("Trevor", 21)};
			
	var qry = from hum in humans
		group hum by hum.Age into grp
		select new{ag=grp.Key, co=grp.Count()};
	foreach (var rec in qry)
	{
		Console.WriteLine("Age={0}; Count={1}", rec.ag, rec.co);
	}
	
}

// Define other methods and classes here
public class Human
{
	private string sName = string.Empty;
	private int iAge = 0;
	
	public Human(string aName, int aAge)
	{
		sName=aName;
		iAge=aAge;
	}
	
	public string Name
	{
		get{return sName;}
		set{sName = value;}
	}

	public int Age
	{
		get{return iAge;}
		set{iAge = value;}
	}	
}


Result:
Age=25; Count=7
Age=24; Count=2
Age=21; Count=2
Age=20; Count=1
Age=23; Count=2


As you can see, you're able to group data depending on your needs.
 
Share this answer
 
v3
Hi,
Go through this-

count-rows-in-linq[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900