【发布时间】:2025-11-28 03:45:02
【问题描述】:
我使用 Linq(连同 EF)来访问我的数据库。我有对象“Job”,它包含几个属性,其中一些是“复杂的”。我的目标是按这些属性对作业进行分组,并对每个组进行计数。
这是我的对象(简化):
public class Job
{
[Key]
public int Id
{
get;
set;
}
[Required]
public Salary Salary
{
get;
set;
}
[Required]
public ICollection<Category> Categories
{
get;
set;
}
}
“Category”是一个复杂的类,看起来像这样:
public class Category
{
[Key]
public int Id
{
get;
set;
}
public Industry Industry //Example: Software
{
get;
set;
}
public Field Field //Example: .NET
{
get;
set;
}
public Position Position //Example: Developer
{
get;
set;
}
}
Industry、Field、Position 和 Salary 类仅包含“int”id 和“string”名称。
我需要按行业、领域、职位和薪水对工作列表进行分组,并计算每个组的数量。这就是我现在的做法:
var IndustryGroupsQuery = from t in Jobs.SelectMany(p => p.Categories)
group t by new { t.Industry} into g
select new
{
Tag = g.Key.Industry,
Count = g.Count()
};
var FieldsGroupsQuery = from t in Jobs.SelectMany(p => p.Categories)
group t by new { t.Field} into g
select new
{
Tag = g.Key.Field,
Count = g.Count()
};
var PositionsGroupsQuery = from t in Jobs.SelectMany(p => p.Categories)
group t by new { t.Position} into g
select new
{
Tag = g.Key.Position,
Count = g.Count()
};
Jobs.GroupBy(job => job.Salary)
.Select(group => new
{
Tag = group.Key,
Count = group.Count()
}))
这很好用,但我想知道是否有可能以某种方式提高它的性能。
Q1:我认为,一个查询可能会比四个查询执行得更好。是否可以将这些查询合并为一个查询?
Q2:当我要求 Linq 按“行业”分组时,它究竟如何区分一个行业和另一个行业?是否隐式比较记录的键?如果我明确告诉 linq 要按哪个属性分组(例如“id”)会更快吗?
谢谢!
【问题讨论】: