我有一个很大的数据库,我需要分组以查找每个的计数Customer
。这是我的查询:
var statisticList = (from item in Connection.DBConnection.TBStatistics
where item.Date >= startDate && item.Date <= endDate
group item by item.Customer into grp
where grp.Count() > 1
select new
{
Customer = grp.Key,
Count = grp.Count(),
}).ToList();
每个客户有一些其他的属性,如Id
,PhoneNumber
,Address
和...。要访问statisticList
我的表格中每个项目的详细信息,请执行以下操作:
foreach (var Cus in statisticList)
{
var allPlateDetail = (from item in Connection.DBConnection.TBStatistics
where item.Customer == Cus.Customer &&
item.Date >= startDate && item.Date <= endDate
select item).ToList();
//More Code...
}
但这非常慢!我希望将Id
每个项目都包含进来statisticList
以快速找到数据库中的记录。这可能吗?
还是有什么办法可以拥有所有这些属性statisticList
?像子列表一样进入列表?
我找到了答案。我建立了一个班级:
public class StatisticsGroup
{
public IEnumerable<DB.TBStatistic> TBStatisticRecords { get; set; }
public string Customer { get; set; }
public int Count { get; set; }
}
然后我在查询中使用了此类:
var statisticList = (from item in Connection.DBConnection.TBStatistics
where item.Date >= startDate && item.Date <= endDate
group item by item.Customer into grp
where grp.Any()
select new StatisticsGroup
{
Customer = grp.Key,
Count = grp.Count(),
TBStatisticRecords = grp.Select(p=> p)
}).ToList();
现在我已经了解了所有细节TBStatisticRecords
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句