LINQ 和实体框架,获取非映射列的相关行的总和
我想获得申请特定职位的申请人总数,这不应该保存为一列。
我的模型很简单:
我们有职位:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用直接 SQL 和 PIVOT 运算符。这实际上不是 Linq 查询的情况。
Use direct SQL with PIVOT operator. This is really not a case for Linq query.
您可以将其作为 C# 程序粘贴到 LINQPad 中并运行。
结果将是:
You can paste this into LINQPad as C# Program and run.
The result will be:
根据我的经验,您只需将表映射到 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