Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

Trying combination logic in Lists in C#

I am using Lists in C#. I have a class this way:

UserManager {
  User User;
  Other Variables;
}

And the User class has some variables like:

User {
  String Username, Password;
}

Now what I am trying to do is, having a list of UserManagers and finding it by Username and that worked this way:

UserManager U = UM.Find(
  item => item.Username == Username
);

But I would like to employ a check for both Username and Password combination. I am new to C# and confused on how to implement. Tried this but no luck:

// Attempt #1
UserManager U = UM.Find(
  item => item.Username == Username && item => item.Password == Password
);

// Attempt #2
UserManager U = UM.Find(
  item => item.Username == Username, item => item.Password == Password
);

// Attempt #3
UserManager U = UM.Find(
  item => item.Username == Username, item.Password == Password
);

BTW, Username and Password are Strings taken from the user through Console.ReadLine(). Please help me out in proceeding further...

Upvotes: 0

Views: 214

Answers (3)

prthrokz
prthrokz

Reputation: 1150

You can try something like:

if(Users.Any(user => user.UserName.Equals(UserName) &&
                                 user.Password.Equals(Password)) {

     var matchedUser = Users.Where(user => user.UserName.Equals(UserName) &&
                                 user.Password.Equals(Password));

     //....
    }

Upvotes: 0

Steve
Steve

Reputation: 216293

Do not repeat the input parameter item

UserManager U = UM.Find(
  item => item.Username == Username && item.Password == Password
);

Upvotes: 2

Andrew
Andrew

Reputation: 2335

Why not just do

UserManager U = UM.Find(
item => item.Username == Username && item.Password == Password
);

Failing that you could do

UserManager U = UM.Where(
item => item.Username == Username && item.Password == Password
);

Upvotes: 0

Related Questions