我在C#中有以下类:
public class Statistics {
public SellerModel Seller {get; set;}
public ClientModel Client {get; set;}
public ProductModel Product {get; set;}
public int Total1 {get; set;}
public int Total2 {get; set;}
}
public class SellerModel {
public int SellerId {get; set;}
public string SellerName {get; set;}
}
public class ClientModel {
public int ClientId {get; set;}
public string ClientName {get; set;}
}
public class ProductModel {
public int ProductId {get; set;}
public string ProductName {get; set;}
}
我带来了统计数据的在线查询,但根据用户的说法,我必须按“客户和卖家”或“产品和卖家”分组,然后很容易做出决定并写两次以个性化的方式查询,但不是时间可持续,因为在这里我只举两个分组的方式,但在我所做的事情中,有12个课我应该和卖家一起组合,我想制作一个通用的方法,它会发送一些参数,并可以根据我发送的内容进行分组。
我已经做过类似的事了:
public IEnumerable<Statistic> GetStatistic(EnumStatistic enumStatistic) {
//Consult everything I need without grouping
var query = QueryStatistic();
// Switch Decision Statistic
IEnumerable<Statistic> model = null;
switch (enumStatistic) {
case EnumStatistic.Client {
model = QueryStatisticGroup(query, x => x.Client, x => null);
break;
}
case EnumStatistic.Product {
model = QueryStatisticGroup(query, x => null, x => x.Product);
break;
}
}
return model;
}
private IEnumerable<Statistic> QueryStatisticGroup(
IEnumerable<Statistic> query,
Func<Statistic, ClientModel> getClient,
Func<Statistic, ProductModel> getProduct)
{
var model =
from q in query
group q by new
{
Product = getProduct(q),
Client = getClient(q),
Seller = q.Seller,
} into g
select new Statistic
{
Client = g.Key.Client,
Product = g.Key.Product,
Seller = g.Key.Seller,
Total1 = g.Sum(x => x.q.Total1),
Total2 = g.Sum(x => x.q.Total2),
};
return model;
}
有两个问题,它不是按类分组,因为如果它只按卖方分组,结果是相同的,没有任何分组,如果我保持原样,它不会分组,但对象是实例化为“null”,然后,我真的不知道该怎么做。
我在很多方面都尝试过,但我做不到。分组的唯一方法,但只有卖家才这样:
group q by new
{
SellerId = q.Seller.SellerId,
SellerName = q.Seller.SellerName
} into g
但这对我来说似乎不切实际或在时间上可持续发展。
欢迎任何帮助。