views:

134

answers:

3

Hi there im using Linq for check if two user fields correspond for an unique user register in SQL Table, for example

UID : userID PIN : passID

so fields have to be from a single user, i was trying this:

 public bool AutentificacionUsuario(string userID , string password passID)
    {
        USER _userID = _db.USER.FirstOrDefault(uid => uid.siglas == userID);
        USER _passID = _db.USER.FirstOrDefault(pwd => pwd.codigousuario == passID);

but boths returns a USER Table instance from Datacontext (two instead one), i want to match them in lambda expression like if " this userID match passID"

Thanks!

+1  A: 
var usr = db.Users.From(uid => uid.dsiglas == userID && 
            uid.codigousuario == passID).FirstOrDefault();

if(usr != null)
   //Authenticate user here

or

var usr = (from u in db.Users
          where u.dsiglas == userID && uid.codigousuario == passID
          select u).FirstOrDefault()

if(usr != null)
    //Authenticate user here
Micah
+1  A: 

You need to use a Where clause to select the user. I'm unsure if LINQ-2-SQL is what I've been using, but I would have done it like this:

USER u = (
 from u in _db.USER
 where u.uid == userID && u.pwd == password
 select u
).FirstOrDefault()

I hope that code is correct, I don't have access to intellisense on a LINQ project right now to check.

Ray Hidayat
+1  A: 

Why not do this ?

var doesUserExist=db.User.Any(p => p.UserID.Equals(userID) && p.Password.Equals(password));
Perpetualcoder
really I just forget about use one lambda expression for iteration, i was trying to put "=>" and local var at twice. somthing like this:var doesUserExist=db.User.Any(p => p.UserID.Equals(userID)
Angel Escobedo