【问题标题】:Access to Details of each item in groupby访问 groupby 中每个项目的详细信息
【发布时间】:2014-04-13 12:20:57
【问题描述】:

我有一个大型数据库,我需要通过 groupby 来查找每个 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();

每个客户都有一些其他属性,例如IdPhoneNumberAddress 和...。要从我的表中访问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...
            }

但是速度很慢! 我希望statisticList 中的每个项目都有Id 以快速找到我数据库中的记录。这可能吗?

或者有没有办法在statisticList 中拥有所有这些属性?像列表中的子列表?

【问题讨论】:

    标签: c# database linq group-by


    【解决方案1】:

    我在 Linq->Sql 或 EntityFramework 中为零。我将提供一些关于 Linq->Objects 的想法,您可以尝试在 Linq->Sql 中转换它。

    首先where grp.Count() &gt; 1 是昂贵的 O(n) 所以我们使用 grp.Any() 这是 O(1) 操作。然后我们就可以像这样通过GroupBy 获得群组。

    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
                         {
                             Customer = grp.Key,
                             //Count = grp.Count(), Note I think we don't need it we can use GroupItems.Count instead
                             GroupItems = grp.ToList()
                         }).ToList();
    
    foreach (var Cus in statisticList)
    {
        //Do whatever with Cus.GroupItems
    }
    

    不确定这是否适用于 Linq->Sql 或 EntityFramework,如果没有帮助,请致歉。我会删除我的答案。

    【讨论】:

    • 它返回错误“LINQ to Entities 无法识别方法 'System.Collections.Generic.List1[DB.TBStatistic] ToList[TBStatistic](System.Collections.Generic.IEnumerable1[DB.TBStatistic])' 方法,并且此方法无法转换为存储表达式。”但我会改变它,看看它是否有效。谢谢你的回答。
    【解决方案2】:

    我找到了答案。我建了一个类:

    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 中有所有详细信息。

    【讨论】:

    • 您仍然需要按照 Sriram 的回答将 grp.Count() &gt; 1 替换为 grp.Any()
    • @AhmadIbrahim 是的,这是真的。
    • 你为什么坚持“ > 1”比较?! :) .. 只是where grp.Any() 没有&gt; 1
    • 我只是忘记删除 "> 1" :)) .
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多