【问题标题】:group join error加入群组错误
【发布时间】:2011-06-28 14:57:59
【问题描述】:

我需要获取所有未删除的配置文件、成功激活的数量和总体激活。键(将配置文件与日志连接起来是“DeviceClass”字段)。我想出了以下 LINQ to SQL 查询:

var v = from profile in repositoryProfiles.GetAll()
    join logsCounted in
        (
        from log in repositoryLogs.GetAll()
        where log.OperationType == EnrollmentLog.OperationTypeEnum.EnrollDevice
        group log by log.DeviceClass into logs
        select new
        {
            DeviceClass = logs.Key, 
            SuccessfulAmount = logs.Where(log=>string.IsNullOrEmpty(log.Error)).Count(), 
            OverallAmount = logs.Count()
        }
        ) on profile.DeviceClass equals logsCounted.DeviceClass
    where profile.Deleted==false
    select new
    {
        Profile = profile,
        SuccessAmount = logsCounted.SuccessfulAmount,
        TotalAmount = logsCounted.OverallAmount
    };

尝试调用v.ToList() 会导致以下错误:

成员访问 'System.String Key' 的 'System.Linq.IGrouping`2[System.String,CMCore.Data.Logging.IEnrollmentLog]' 类型不合法 'System.Linq.IGrouping`2[System.String,CMCore.Data.Logging.EnrollmentLog]。

问题 1:“Key”属性访问有什么问题?

问题 2:如何实现上述想法?

编辑:

简化版:

        var v1 = from log in repositoryLogs.GetAll()
                 group log by log.DeviceClass
                 into logs
                 select new
                            {
                                DeviceClass = logs.Key
                            };
        var logsGroupped = v1.ToList();

导致同样的错误... :(

【问题讨论】:

  • 忽略之前的评论 :)
  • :) 好的,所以也忽略了我的评论 :)
  • 但是,仅用于历史记录和 ... 'repositoryLogs.GetAll()' 返回 'IEnrollmentLog' 对象的列表。但内部查询应返回具有“DeviceClass”、“SuccessfulAmount”和“OverallAmount”属性的匿名类型对象列表。通过对“log.DeviceClass”字段进行日志分组获得的“日志”列表应该是“IEnrollmentLog”的列表,并且这个“日志”应该有一个“键”属性......应该代表分组的“设备类”值......
  • System.Linq.IGrouping`2[System.String,CMCore.Data.Logging.IEnrollmentLog]
  • 我假设DeviceClassstring。如果您将分组内容更改为仅错误字符串,它是否适用于您的简化示例? ... group log.Error by log.DeviceClass into logs ...

标签: c# sql-server-2005 linq-to-sql .net-3.5


【解决方案1】:

这里有一个解决方案:

    var v1 = from log in repositoryLogs.GetAll()
             group log by log.DeviceClass
             into logs
             select new
                        {
                            DeviceClass = logs.First().DeviceClass
                        };
    var classes = v1.ToList();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 1970-01-01
    • 2019-04-23
    相关资源
    最近更新 更多