Madam Zu Zu
Madam Zu Zu

Reputation: 6615

Comparing two counts in LINQ

hi have a simple query that compares all sent out requests to those that have been downloaded.

i have a tsql that works perfectly like so:

select case when (select COUNT(*) from FileRecipient where File_ID = 3 and downloaded = 'Y') 
= (select COUNT(*) from FileRecipient where File_ID = 3) then 'Y' else 'N' end

but would like to do it in LINQ.

any and all help would be appreciated. i tried the following, but obviously i'm a long way away from getting it right. this just gives me the first of the two counts. do i need to do two separate statements to get the second?

public static bool DownloadedByAll(int fsID)
{
    using (SEntities ctx = CommonS.GetSContext())
    {
        var result = (from fr in ctx.FileRecipients
                      where fr.File_ID == fsID && fr.downloaded == "Y"
                      select fr.Recipient_ID).Count();
    }
}

or would it just be simpler for me to use tsql within the entity framework for this?

Upvotes: 1

Views: 404

Answers (1)

pquest
pquest

Reputation: 3290

This should get you the result you need

var result = ctx.FileRecipients
                .Count(f=>f.File_ID == 3 
                       && f.downloaded == 'Y') == ctxt.FileRecipients
                                                      .Count(f=>f.File_ID == 3);

Upvotes: 5

Related Questions