我有以下代码从Entify Framework DB中提取数据。
return r.Find()
.GroupBy(x => x.ProductID)
.Select(x => new ProductCount
{
ProductID = x.Key,
Quantity = x.Count()
})
.ToList();
如您所见,数据在ProductID上分组,并返回计数。产品是买方的子女,也属于同一实体,因此我想知道是否有可能在一次交易中返回产品级别和买方级别的计数。
进入如下的课程:
public class BuyerAndProductCount
{
public int BuyerID { get; set; }
public int BuyerCount { get; set; }
public int ProductID { get; set; }
public int ProductCount { get; set; }
}
更新
这是正在查询的实体。
public partial class ApplicationHistory
{
public int ApplicationID { get; set; }
public decimal Commission { get; set; }
public long ResponseTimeMS { get; set; }
public string ResponseInfo { get; set; }
public System.Guid ApplicantID { get; set; }
public int ApplicationResultID { get; set; }
public int BuyerID { get; set; }
public int ProductID { get; set; }
public System.DateTime ExpiresOn { get; set; }
public System.DateTime CreatedOn { get; set; }
public virtual Application Application { get; set; }
public virtual Applicant Applicant { get; set; }
public virtual Product Product { get; set; }
}
答案 0 :(得分:2)
您可以使用GroupBy
方法中的匿名类型进行示例:
return r.Find()
.GroupBy(x => new { x.ProductID, x.BuyerID })
.Select(x => new ProductCount
{
ProductID = x.Key.ProductID,
Quantity = x.Count()
})
.ToList();