I have a function that queries a database and returns a list:
return roles.Where(r => (rolesForUser.Contains(r.Type.ToString()) ||
rolesForUser == null).OrderBy(r => r.Description).ToList();
How does that even compile? You have mis-matched parens.
When I don't want to account this for filter I just pass null but I
get an error.
Can't I pass a string[] as null?
How should I pass it if I don't want to pass any data and what should
I use to replace the condition:
rolesForUser == null
From the code you posted (such as it is), it seems to me that if you just
switched the two clauses in your condition so that it reads "rolesForUser
== null || rolesForUser.Contains(r.Type.ToString())" instead, your error
would go away. Hard to say for sure though, since you didn't bother to
say anything about the actual error you're getting. And since we don't
know what output you actually want, just avoiding the error isn't
necessarily the whole answer.
More generally, you need to get into the habit of asking better
questions. You didn't post barely any code (and not any that would show
how the parameter is passed or used), you didn't describe the exact error
you got, nor did you describe what behavior you would expect when you pass
null.
If you ask a garbage question, the best you can hope for is a garbage
answer. Sometimes a person's psychic debugging skills kick in and you get
lucky. But you shouldn't count on it, and in any case garbage questions
make it seems as though you don't really care enough about the answer to
put any work into the question. Surely that's not the impression you
intend.
Pete
Pete,
Calm down ... When I post full code I hardly get an answer so I tend
to extract the part where I detect the error.
The rules depend on the forum ... in CSS forums if I post my full code
I have complains all the way ...
In fact it is easier for me to just post my code and say what error it
is.
That said, thank you for your help, the suggestion of switching worked
fine. I didn't though of that because all my other conditions where on
that order and they were working as expected ... the conditions where
different ...
Anyway, here is my code:
public static IList<Role> List(CultureInfo culture, RoleType?
type, bool? open, bool? beginWithEmpty, string[] rolesForUser) {
IList<Role> roles = new List<Role>();
if (beginWithEmpty ?? false) roles.Add(new Role { Type = null,
Description = "---", Open = true });
switch (culture.TwoLetterISOLanguageName.ToLower()) {
case "pt":
roles.Add(new Role { Type = RoleType.Administrator,
Description = "Administrador", Open = false });
roles.Add(new Role { Type = RoleType.Collaborator,
Description = "Colaborador", Open = false });
roles.Add(new Role { Type = RoleType.Professor, Description
= "Professor", Open = true });
break;
}
return roles.Where(r => (rolesForUser == null ||
rolesForUser.Contains(r.Type.ToString())) && (open == null || r.Open
== open) && (type == null || r.Type == type)).OrderBy(r =>
r.Description).ToList();
}
I appreciate all the help I get and if I don't explain my problems as
expected is really because I think I am posting it the right way.
Thank You,
Miguel