tags:

views:

48

answers:

5

My table has the following columns:

User (UserID, GroupID, userName, userType, ...)

I need to do this:

SELECT GroupID
FROM Users
WHERE userID = @userID AND username = @username and usertype = @usertype.

I have my DataContext ready to go, guidance on my LINQ query would be great!

A: 

You should download LinqPad and connect to your data source with it. This will really help you get going with LINQ. I also suggest buying the full version ($19) of LinqPad b/c the intellisense can really help you in the beginning

Cody C
A: 
int UserID = [code to get userID],
    Usertype=[code to get usertype];
string Username=[code to get username];

from u in Users
 where u.userID = UserID
  && username=Username
  && usertype=Usertype
select u.GrupID;
tsilb
A: 

Roughly...

int ID = 1;
string uname = "whatever";
string utype  = "whatever"

User myUser = (from u in Users where u.userID == ID && u.username == uname && u.usertype == utype select u).SingleOrDefault();

But yes, grab linqpad

Allen
A: 
var result = from u in users
             where u.UserID == userID
             && u.UserName == userName
             select u.GroupID
Tony D
A: 

I am not sure if you wanted just one result or a list. I am assuming only one will be found since you are searching on a userID.

string userID = "Your user ID here";
string userName = "Your username here";
string userType = "Your usertype here";
var groupID = (from user in dataContext.Users
               where user.UserID == userID &&
                     user.UserName == userName &&
                     user.UserType == userType
               select user.GroupID).FirstOrDefault();

If you want a list of all the rows that match you could do a ToList() at the end as well.

I haven't tested the code so it could have some typos and I wasn't sure what your variable names were for from your dbml.

Kelsey
@mrblah if this answer or another helped you, you should set an accepted answer so that others reading this question can locate the solution that helped you.
Kelsey