I'll just attach a picture for reference on this one. I am stumped. In the debugger, the values definitely equal each other, but Find<T>
is still returning null and Exists<T>
is still returning false. For reference: UserRepository
implements IEnumerable<T>
where T
is DomainUser
.
-
Try converting to a byte array and checking the byte-codes. I'm guessing one of them has an appended '0' byte.– AlexJul 10, 2013 at 15:06
2 Answers
The problem is that the type of CommandArgument
is object
, so it's performing a reference identity check. (I'm surprised this isn't giving you a compile-time warning.)
You could either cast CommandArgument
to string
, or use Equals
:
u => u.Username == (string) args.CommandArgument
or
u => Equals(u.Username, args.CommandArgument)
(Using the static Equals
method this way means it'll work even for users with a null
username, unlike u.Username.Equals(args.CommandArgument)
.)
I wouldn't convert the sequence to a list though - I'd just use LINQ instead:
DomainUser toRemove =
repo.FirstOrDefault(u => u.Username == (string) args.CommandArgument);
-
Thanks for pointing out that I can use
FirstOrDefault
without callingToList
. I guess I had only looked at the type ofCommandArgument
on theControl
(it isString
) and not theRepeaterCommandEventArgs
. Jul 10, 2013 at 15:10