-1

I have some method with linq query. Logic is next, if I pass null for list of role ids, I want all roles to be included in process, but if it has value, I want only those with id in the list. I am receiving exeption.

public static List<NameEmail> GetNameEmailPairs(Guid guid, List<int> recipientRoles)
        {
            using (var tc = TransactionContext())
            {
                var dc = tc.DataContext;

                var nameEmailPairs = (
                                 from email in dc.Emails
                                 join logon in dc.Logons on email.GUID equals logon.GUID                                     
                                 join eLogon in dc.ELogons on logon.UID equals eLogon.LogonGUID                         
                                 join role in dc.Roles on entityLogon.PrimaryPermissionRoleID equals role.RoleID
                                 where
                                     (recipientRoles == null || recipientRoles.Contains(role.RoleID))

                                 select new NameEmail
                                            {
                                                Email = email.EmailAddress,
                                                FullName = GetName(logon.GUID)
                                            } 
                             )
                    .ToList();
                return nameEmailPairs;
            }
        }

This part is breaking (recipientRoles == null || recipientRoles.Contains(role.RoleID)). Can you please help me with this, what should I do?

4

1 回答 1

2

为了使调试更容易,将您的查询分解成如下部分:

var nameEmailPairs = (
  from email in dc.Emails
  join logon in dc.Logons on email.GUID equals logon.GUID                                     
  join eLogon in dc.ELogons on logon.UID equals eLogon.LogonGUID                         
  join role in dc.Roles on entityLogon.PrimaryPermissionRoleID equals role.RoleID
  select new {email, role}
);

if(recipientRoles != null)
{
    nameEmailPairs = nameEmailPairse.Where(
        recipientRoles.Contains(p => p.role.RoleID)
    );
}

var nameEmailPairs = (from p in nameEmailPairs
                     select new NameEmail
                                {
                                  Email = email.EmailAddress,
                                  FullName = GetName(logon.GUID)
                                }).ToList();
于 2012-07-05T17:42:53.187 回答