LINQ和实体框架,获取非映射列的相关行的总和

时间:2011-10-13 09:15:12

标签: linq entity-framework linq-to-sql linq-to-entities entity-framework-4.1

我想获得应用于特定职位的申请人总数,不应将其保存为专栏。

我的模型很简单:

我们有职位:

  • net developer

  • java开发人员

我们有申请人:

  • 路易斯
  • 约翰

我们有每个职位的申请人

使用此列或属性,我想知道有多少人已应用于每个位置,具体取决于状态。 所以在我的mvc视图中,我希望显示类似的内容:

应用的位置已接受拒绝...其他状态

.net developer 5 3 2

java developer 3 2 1

这里真正的问题是我不是很专业的linq查询。

编辑:我想我需要更改linq查询必须编码的位置,我想它应该在ApplicantPosition类而不是Position中,我还将Position和Application的类型更改为ICollection。

请参阅修改后的代码。

 public class Position
    {
        public int id { get; set; }

        [StringLength(20, MinimumLength=3)]
        public string name { get; set; }
        public int yearsExperienceRequired { get; set; }


    }

    public class Applicant
    {
        public int ApplicantId { get; set; }
        [StringLength(20, MinimumLength = 3)]
        public string name { get; set; }
        public string telephone { get; set; }
        public string skypeuser { get; set; }
        public ApplicantImage photo { get; set; }
    }

public class ApplicantPosition
{
    public virtual ICollection<Position> appliedPositions { get; set; }
    public virtual ICollection<Applicant> applicants { get; set; }
    public DateTime appliedDate { get; set; }
    public int StatusValue { get; set; }

    public Status Status
    {
        get { return (Status)StatusValue; }
        set { StatusValue = (int)value; }
    }

      [NotMapped]
    public int numberOfApplicantsApplied
    {
        get
        {
            var query = 
                from ap in appliedPositions 
                select new
                {
                    positionName = g.Key.name,
                    peopleApplied = g.Count(x => x.Status == Status.Applied),
                };
            return query.Count(); ---??
        }
    }
}

3 个答案:

答案 0 :(得分:2)

将直接SQL与PIVOT operator一起使用。 Linq查询确实不是这种情况。

答案 1 :(得分:1)

根据我的经验,您只需将表格映射到DBML到实体框架模型文件即可使用LinQ或实体框架。 换句话说,Microsoft为您提供了一个动态LinQ类,您可以使用它。我认为您映射了所有列和用户Dynamic LinQ类。祝你好运

答案 2 :(得分:1)

您可以将其作为C#程序粘贴到LINQPad中并运行。

public enum  Status
{
    Applied, 
    Accepted,
    Rejected
}

public class Position
{
    public int id { get; set; }
    public string name { get; set; }
}

public class Applicant
{
    public int ApplicantId { get; set; }
    public string name { get; set; }
}

public class ApplicantPosition
{
    public Position appliedPosition { get; set; }
    public Applicant applicant { get; set; }
    public DateTime appliedDate { get; set; }
    public Status Status { get; set; }
}


void Main()
{
    var p1 = new Position { id = 1, name = ".net developer" };
    var p2 = new Position { id = 2, name = "java developer" };

    var a1 = new Applicant { ApplicantId = 100, name = "Luis" };
    var a2 = new Applicant { ApplicantId = 200, name = "John" };

    var ap1 = new ApplicantPosition { appliedPosition = p1, applicant = a1, Status = Status.Applied };
    var ap2 = new ApplicantPosition { appliedPosition = p1, applicant = a2, Status = Status.Accepted };
    var ap3 = new ApplicantPosition { appliedPosition = p2, applicant = a2, Status = Status.Rejected };

    var db = new[] { ap1, ap2, ap3};

    var query = 
        from ap in db 
        group ap by ap.appliedPosition into g
        select new
        {
            positionName = g.Key.name,
            peopleApplied = g.Count(x => x.Status == Status.Applied),
            peopleAccepted = g.Count(x => x.Status == Status.Accepted),
            peopleRejected = g.Count(x => x.Status == Status.Rejected),
        };

    query.Dump();
}

结果将是:

positionName     peopleApplied peopleAccepted peopleRejected
.net developer   1             1              0
java developer   0             0              1