none
simple Linq query to return just an id RRS feed

  • Question

  •  

     

    Hi there, Im trying to get an employeeId which is an integer from a linq query as below but not sure how to:-

    var employee = from e in _db.t_employee
    where e.employee_NT_Login == "domain\\username"
    select {e};

    I have tried something like this:-

    var employee = from e in _db.t_employee
    where e.employee_NT_Login == "domain\\username"
    select new {e.employee_id };

    Can someone steer me in the right direction please?

    Sunday, February 13, 2011 6:58 PM

Answers

  • Hi Gregor;

    The following code snippet should return the employee Id as an integer. If the employee_NT_Login is not found a zero is returned otherwise the employeeId is returned. 

    string name = "domain1\\TheUsername";
    
    int employee = (from e in _db.t_employee             
            where e.employee_NT_Login == name
            select e.employee_id).FirstOrDefault();
    

    Fernando
    Sunday, February 13, 2011 10:11 PM

All replies

  • Hi there, Im trying to get an employeeId which is an integer from a linq query as below but not sure how to:-

    var employee = from e in _db.t_employee
    where e.employee_NT_Login ==
    "domain\\username"
    select
    {e};

    I have tried something like this:-

    var employee = from e in _db.t_employee
    where e.employee_NT_Login == "domain\\username"
    select new {e.employee_id };

    Can someone steer me in the right direction please?

    Sunday, February 13, 2011 6:59 PM
  • Hi Gregor;

    The following code snippet should return the employee Id as an integer. If the employee_NT_Login is not found a zero is returned otherwise the employeeId is returned. 

    string name = "domain1\\TheUsername";
    
    int employee = (from e in _db.t_employee             
            where e.employee_NT_Login == name
            select e.employee_id).FirstOrDefault();
    

    Fernando
    Sunday, February 13, 2011 10:11 PM
  • Hi Gregor;

    The following code snippet should return the employee Id as an integer. If the employee_NT_Login is not found a zero is returned otherwise the employeeId is returned. 

    string
     name = "domain1\\TheUsername"
    ;

    int employee = (from e in _db.t_employee
    where e.employee_NT_Login == name
    select e.employee_id).FirstOrDefault();

    Same as in the last thread.

    Fernando

    Sunday, February 13, 2011 10:31 PM
  • Sorry but that formatting was bad, here is another try.

    The following code snippet should return the employee Id as an integer. If the employee_NT_Login is not found a zero is returned otherwise the employeeId is returned.

    string name = "domain1\\TheUsername";
    
    int employee = (from e in _db.t_employee
            where e.employee_NT_Login == name
            select e.employee_id).FirstOrDefault();
    
    Same as in the last thread.

    Sunday, February 13, 2011 10:35 PM
  • Hi Gregor,

     I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 
     
    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 17, 2011 2:37 AM
    Moderator
  • alen, I just wanted to let you know that the code you presented here worked. Thank you very much for that! Before i bumped onto this thread what i used to do is whenever i needed an Id to be returned from a linq query i used to iterate thru the result and add all elements (usually 1) to a list and then extract the first element from it. :)) Thank you again!
    Sunday, May 15, 2011 6:23 PM