【问题标题】:Convert Select/Count SQL Command to Linq to Entities将 Select/Count SQL 命令转换为 Linq 到实体
【发布时间】:2013-05-19 14:15:24
【问题描述】:

我对使用 Linq 到实体的计数命令有疑问。 我想做的是用我的员工公民身份来统计所有的性别

公民身份 0 - all ; 1 = single ; 2 - married

我是 linq to entity 的新手 这是我的 SQL 命令代码。

我不知道如何转换这个。

select '0' as UserID, COUNT(a.Gender) as TotalCount from Employees a 
union all
select '1' as UserID, COUNT(a.Gender) as TotalCount from Employees a where a.CivilStatus = 1
union all
select '2' as UserID, COUNT(a.Gender) as TotalCount from Employees a where a.CivilStatus = 2

像这样

UserID | TotalCount
0     | 10
1     | 7
2     | 3

提示
假设我有 10 名员工(男性和女性),我想知道其中有多少是单身或已婚。

谢谢。 =)

【问题讨论】:

  • 仅供参考,看看linqpad.net它可能会帮助你
  • 我可以帮助你,但我不明白这个问题......你想计算所有可能的性别吗?我可以很简短地回答这个问题:2...请更准确!
  • 它应该计算所有civilstatus的totalcount,值为1;和值为 2
  • @jaydotnet 我更新了问题

标签: sql select count linq-to-entities


【解决方案1】:

此 linqpad 程序按公民身份对员工进行分组,然后计算所有成员以及男性和女性,我认为这表明了您所追求的分组。

void Main()
{
    var employees = new employee[] { new employee {Id=1,CivilStatus=1, Gender='M'},
    new employee {Id=2,CivilStatus=1, Gender='M'},
    new employee {Id=3,CivilStatus=2, Gender='F'},
    new employee {Id=4,CivilStatus=1, Gender='F'},
    new employee {Id=5,CivilStatus=2, Gender='F'},
    new employee {Id=6,CivilStatus=1, Gender='M'},
    new employee {Id=7,CivilStatus=2, Gender='M'},
    new employee {Id=8,CivilStatus=2, Gender='M'},
    new employee {Id=9,CivilStatus=2, Gender='M'},
    new employee {Id=9, Gender='M'}};

    var result = from x in employees 
    group x by x.CivilStatus into g
    select new { CivilStatus = g.Key,
                    All=g.Count(), 
                    Male=g.Count(e => e.Gender=='M'), 
                    Female=g.Count(e => e.Gender=='F') } ;
    result.Dump();

}

class employee
{
    public int Id {get;set;}
    public int CivilStatus {get;set;}
    public char Gender {get;set;}
}

【讨论】:

  • 这不是我要找的。我想要的是获得状态为 1 或 2 或全部的所有性别(即使是男性或女性)的总数
  • 假设我有 10 名员工(男性和女性),我想知道其中有多少是单身或已婚。
  • 它确实为您提供了这些信息以及更多信息。如果您删除两行 Male=g.Count(e => e.Gender=='M') 和 Female=g.Count(e => e.Gender=='F') 结果正是您所拥有的要求。
  • 它给了我 1 1 1 1 1 1 1 价值
  • 尝试下载 linqpad 并将该代码直接粘贴到其中并作为 c# 程序运行。我得到:{CivilStatus=1,Count=4},{CivilStatus=2,Count=5},{CivilStatus=0,Count=1}
【解决方案2】:

试试这个:

var q1 = from a in context.Employees 

                    group a by 1 into g
            select new
            {
                UserID = 0,
                TotalCount = g.Count(),
            };
        var q2 = from a in context.Employees 
                 where a.CivilStatus = 1
                 group a by 1 into g
                 select new
                 {
                     UserID = 1,
                     TotalCount = g.Count(),
                 };
        var q3 = from a in context.Employees 
                 where a.CivilStatus = 2
                 group a by 1 into g
                 select new
                 {
                     UserID = 2,
                     TotalCount = g.Count(),
                 };
        var result = q1.Union(q2).Union(q3).ToList();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多