开发者

group join error

开发者 https://www.devze.com 2023-02-12 00:56 出处:网络
I need to fetch all non-deleted profiles, amounts of successful and overall activations. The key (which connects profiles with logs is \'DeviceClass\' field). I came up with the following LINQ to SQL

I need to fetch all non-deleted profiles, amounts of successful and overall activations. The key (which connects profiles with logs is 'DeviceClass' field). I came up with the following LINQ to SQL query:

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
    };

Attempting to call v.ToList() causes the following error:

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].

Question 1: What is wrong with 'Key' property access?

Question 2: How could I implement the above mentioned idea?

EDIT:

The simplified version:

        var v1 = from log in repositoryLogs.GetAll()
                 group log by log.DeviceClass
                 into logs
        开发者_如何学Python         select new
                            {
                                DeviceClass = logs.Key
                            };
        var logsGroupped = v1.ToList();

causes the same error... :(


Here is a solution:

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

精彩评论

暂无评论...
验证码 换一张
取 消