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

发布于 2024-12-09 14:41:26 字数 1940 浏览 4 评论 0原文

我想获得申请特定职位的申请人总数,这不应该保存为一列。

我的模型很简单:

我们有职位:

  • net 开发人员

  • java 开发人员

我们有申请人:

  • Luis
  • John

我们每个职位都有申请人

对于此栏目或属性,我想要根据状态了解有多少人申请了每个职位。 因此,在我的 mvc 视图中,我想显示如下内容:

Position Applied Accepted Rejected ... other status

.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(); ---??
        }
    }
}

I want to get the sum of applicants that applied to a specific position, this should not be saved as a column.

My model is simple:

We have positions:

  • net developer

  • java developer

We have applicants:

  • Luis
  • John
  • etc

We have applicants per position

With this column or property I want to know how many people have applied to each position, depending on the status.
So in my mvc view I want to show something like:

Position Applied Accepted Rejected ... other status

.net developer 5 3 2

java developer 3 2 1

The real problem here is the linq query which I am not very expert.

EDIT: I think I needed to change where the linq query must be coded, I suppose it should be in the ApplicantPosition class instead of Position, I also changed the types of Position and Application to be ICollection.

Please see the modified code.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情释 2024-12-16 14:41:26

使用直接 SQL 和 PIVOT 运算符。这实际上不是 Linq 查询的情况。

Use direct SQL with PIVOT operator. This is really not a case for Linq query.

青瓷清茶倾城歌 2024-12-16 14:41:26

您可以将其作为 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

You can paste this into LINQPad as C# Program and run.

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

The result will be:

positionName     peopleApplied peopleAccepted peopleRejected
.net developer   1             1              0
java developer   0             0              1
酒中人 2024-12-16 14:41:26

根据我的经验,您只需将表映射到 DBML 到实体框架模型文件即可使用 LinQ 或实体框架。
以其他方式,微软为您提供了一个动态LinQ类,您可以使用它。我认为您映射了所有列和用户动态LinQ类。祝您好运

According to my experiences you can use LinQ or Entity Framework just by mapping your tables in to a DBML to a Entity Framework Model file.
In other way Microsoft gives you a Dynamic LinQ class that you can use it.I think you map all your columns and user Dynamic LinQ class.Good luck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文