我有以下实体模型。为了简洁起见,我省略了每个实体的很多属性。
public sealed class Platform {
/// <summary>
/// Get and Set Platform's Unique Identifier.
/// </summary>
public int Id { get; set; }
/// <summary>
/// Determine if an Object is Equal to This Platform.
/// </summary>
/// <param name="obj">
/// An object to compare.
/// </param>
/// <returns>
/// A boolean true if the object is equal to this platform. A boolean false otherwise.
/// </returns>
public override bool Equals(object obj) {
bool isObjectPlatform = obj is Platform;
bool isObjectIdEqual = isObjectPlatform && (obj as Platform).Id == this.Id;
return isObjectIdEqual;
}
/// <summary>
/// Get Platform's Hash Code.
/// </summary>
/// <returns>
/// The platform's hash code, equalling the platform's unique identifier.
/// </returns>
public override int GetHashCode() {
return this.Id;
}
}
public sealed class Capture {
/// <summary>
/// Get and Set Capture's Unique Identifier.
/// </summary>
public int Id { get; set; }
/// <summary>
/// Get and Set Capture's Platform.
/// </summary>
public Platform Platform { get; set; }
/// <summary>
/// Determine if an Object is Equal to This Capture.
/// </summary>
/// <param name="obj">
/// An object to compare.
/// </param>
/// <returns>
/// A boolean true if the object is equal to this capture. A boolean false otherwise.
/// </returns>
public override bool Equals(object obj) {
bool isObjectCapture = obj is Capture;
bool isObjectIdEqual = isObjectCapture && (obj as Capture).Id == this.Id;
return isObjectIdEqual;
}
/// <summary>
/// Get Capture's Hash Code.
/// </summary>
/// <returns>
/// The capture's hash code, equalling the capture's unique identifier.
/// </returns>
public override int GetHashCode() {
return this.Id;
}
}
我基本上想要的是一个 LINQ 查询,当然,它由一个 EF 支持,这将使我得到按整个平台实体分组的捕获计数。我希望能够返回对每个Platform的引用,而不是它的一个属性,以及与之关联的每个Capture的计数。
我做了这个查询,效果很好:
var query = this._defaultContext.Captures
.Include(m => m.Platform)
.GroupBy(m => m.Platform.Id)
.Select(m => new {
PlatformId = m.Key,
Count = m.Count()
});
但如您所见,我是按平台的 Id属性分组的。我宁愿拥有的是:
var query = this._defaultContext.Captures
.Include(m => m.Platform)
.GroupBy(m => m.Platform)
.Select(m => new {
Platform = m.Key,
Count = m.Count()
});
这不起作用。它只是为数据库中的每条记录计数 1。看起来它不知道按整个实体分组。我希望它可以通过GetHashCode和Equals方法实现来区分每个平台,但没有运气。
有人遇到过这样的场景吗?有什么办法吗?还是我必须手动完成。我讨厌这样,因为它可能会导致某种 N + 1 查询。
提前致谢。