0

問題: Linqでこれを書き直そうとしています:

listOfUsersForReviewerはIEnumerable<User>

int countOfGreenUsers = 0;
foreach (var user in listOfUsersForReviewer)
{
    var u = (from reviewitems in context.ReviewItems
                join groupaccountlinks in context.GroupAccountLinks on reviewitems.GroupAccountID equals groupaccountlinks.GroupAccountID
                join reviews in context.Reviews on reviewitems.ReviewID equals reviews.ReviewID
                join applications in context.Applications on reviews.ApplicationID equals applications.ApplicationID
                join reviewers in context.Reviewers on applications.ResponsibleReviewerID equals reviewers.ReviewerID
                join accounts in context.Accounts on groupaccountlinks.AccountID equals accounts.AccountID
                join users in context.RBSUsers on accounts.UserID equals users.UserID
                where
                    users.UserID == user.UserID &&
                    reviewers.FullyQualifiedLogin == fullyQualifiedLogin &&
                    reviews.ReviewStatusID == (byte)Enums.ReviewStatus.InProgress &&
                    reviewitems.ReviewItemStatusID == (byte)Enums.ReviewItemStatus.Unapproved
                select reviewitems);

    byte colour = (byte)Enums.UserStatusColour.Red;
    if (u.Count() == 0)
    {
        colour = (byte)Enums.UserStatusColour.Green;
        countOfGreenUsers++;
    }
}

匿名型を作成しようとしましたが、これはコンパイルされません。

// select number of green users
var x = from user in listOfUsersForReviewer
        from reviewitems in context.ReviewItems
        join groupaccountlinks in context.GroupAccountLinks on reviewitems.GroupAccountID equals
            groupaccountlinks.GroupAccountID
        join reviews in context.Reviews on reviewitems.ReviewID equals reviews.ReviewID
        join applications in context.Applications on reviews.ApplicationID equals applications.ApplicationID
        join reviewers in context.Reviewers on applications.ResponsibleReviewerID equals
            reviewers.ReviewerID
        join accounts in context.Accounts on groupaccountlinks.AccountID equals accounts.AccountID
        join users in context.RBSUsers on accounts.UserID equals users.UserID
        where
            users.UserID == user.UserID &&
            reviewers.FullyQualifiedLogin == fullyQualifiedLogin &&
            reviews.ReviewStatusID == (byte)Enums.ReviewStatus.InProgress &&
            reviewitems.ReviewItemStatusID == (byte)Enums.ReviewItemStatus.Unapproved
        select new
            {
                UserID = user.UserID,
                CountOfGreen = reviewitems.Count()
            };
4

2 に答える 2

0

Add grouping clause

var x = from user in listOfUsersForReviewer
        from reviewitems in context.ReviewItems
        join groupaccountlinks in context.GroupAccountLinks on reviewitems.GroupAccountID equals
            groupaccountlinks.GroupAccountID
        join reviews in context.Reviews on reviewitems.ReviewID equals reviews.ReviewID
        join applications in context.Applications on reviews.ApplicationID equals applications.ApplicationID
        join reviewers in context.Reviewers on applications.ResponsibleReviewerID equals
            reviewers.ReviewerID
        join accounts in context.Accounts on groupaccountlinks.AccountID equals accounts.AccountID
        join users in context.RBSUsers on accounts.UserID equals users.UserID
        where
            users.UserID == user.UserID &&
            reviewers.FullyQualifiedLogin == fullyQualifiedLogin &&
            reviews.ReviewStatusID == (byte)Enums.ReviewStatus.InProgress &&
            reviewitems.ReviewItemStatusID == (byte)Enums.ReviewItemStatus.Unapproved
        group user by user.UserID into grouping
        select new
            {
                UserID = grouping.Key,
                CountOfGreen = grouping.Count()
            };
于 2013-01-10T18:41:55.913 に答える
0

ロジックを再考し、このクエリを 2 つの単純なクエリに分割しました。

私が使用したツールは次のとおりです。SQL Server Management Studio (グラフィカル表現)、Linqer、場合によってはLinqpad

また、クエリを英語で書くことも大いに役立ちました。

于 2013-01-11T16:56:21.323 に答える