如何使用LINQ COMMAND来计算记录 [英] how do I use LINQ COMMAND to count records

查看:72
本文介绍了如何使用LINQ COMMAND来计算记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是asp代码

here is asp code

<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# CODE

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();


          }
       }





我想得到总额没有。具有相同EmpStatus_Id的员工然后在相应的文本框中显示。

来自上面的代码我在表格中没有员工反对从下拉列表中选择部门。

代码流程从下拉菜单中选择部门的方式。我们得到该部门的总员工,然后在文本框中没有缺席,休假,医疗等。



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.

推荐答案

我建议下载它: C#中的101个LINQ样本 [ ^ ]。在那里你会找到你想要的东西;)



如需了解更多信息,请参阅:

如何:用C#编写LINQ查询 [ ^ ]

如何:使用LINQ(Visual Basic)计数,求和或平均数据 [ ^ ] - 这就是你需要的;)

收集查询 [ ^ ]



我使用 LinqPad [ ^ ]相当十。它可能会帮助您编写正确的查询。



示例:
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:
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.



通过这个 -



count-rows-in-linq [ ^ ]


这篇关于如何使用LINQ COMMAND来计算记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆