组加入错误

时间:2011-02-16 19:42:15

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

我需要获取所有未删除的配置文件,成功和整体激活的数量。密钥(将配置文件与日志连接起来是'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()会导致以下错误:

Member access 'System.String Key' of  
'System.Linq.IGrouping`2[System.String,CMCore.Data.Logging.IEnrollmentLog]'  
not legal on type  
'System.Linq.IGrouping`2[System.String,CMCore.Data.Logging.EnrollmentLog].

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

问题2 :我怎样才能实现上述想法?

修改

简化版:

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

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

1 个答案:

答案 0 :(得分:0)

这是一个解决方案:

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