Reputation: 4015
I am fetching userID and userRole using LINQ query and I am returning them back. I want to know how can I read each value after returning data. Here's the code.
public object[] GetUserInfoFromUsernameAndPassword(string userName, string password)
{
var userInfo = from u in STE.tblUsers
where u.UserName == userName
&& u.PWD == password
select new
{
u.UserID,
u.UserRole
};
return userInfo.ToArray();
}
Is this a good way of doing it? If anyone know better way, please share..
Thanks
Upvotes: 0
Views: 14854
Reputation: 4015
For those who are looking for a way to do this without creating a new class, use either out
or ref
parameters-
public void GetUserInfoFromUsernameAndPassword(string userName, string password, out int userID, out string userRole)
{
var userInfo = from u in STE.tblUsers
where u.UserName == userName
&& u.PWD == password
select new
{
userID = u.UserID,
userRole = u.UserRole
};
}
Call it from your code as-
string username = "abc";
string password = "***";
string uid, role;
GetUserInfoFromUsernameAndPassword(username, password, out uid, out role);
Console.WriteLine(uid);
Console.WriteLine(role);
Upvotes: 4
Reputation: 4394
This code:
select new
{
u.UserID,
u.UserRole
};
creates an anonymous class that can be used only inside GetUserInfoFromUsernameAndPassword
method.
So to use return values later, you need to create some class and return a collection of objects of this class:
public class UserInfo
{
public int Id{get; set;}
public string Role{get; set;}
}
and then you need to fix your query:
var userInfo = from u in STE.tblUsers
where u.UserName == userName
&& u.PWD == password
select new UserInfo
{
Id = u.UserID,
Role = u.UserRole
};
and the last thing is to change the return type of the method:
public UserInfo[] GetUserInfoFromUsernameAndPassword(string userName, string password)
After that you will be able to iterate through the collection using foreach
e.g:
var users = GetUserInfoFromUsernameAndPassword("username", "password");
foreach(var user in users)
{
Console.WriteLine("{0} - {1}", user.Id, user.Role);
}
Upvotes: 6
Reputation: 49115
Since Anonymous Objects aren't accessible outside the scope of their defining method, you'll have to use dynamic
:
var data = GetUserInfoFromUsernameAndPassword(username, password);
foreach (dynamic item in data)
{
var userID = item.UserID;
var userRole = item.UserRole;
}
You can also use Reflection, but dynamic
is much easier and flexible.
See Anonymous Types
Upvotes: 1
Reputation: 892
first of all excuse me for rewriting your code a little - I'm completely against using the 'SQLified' Linq code in C#. In order to iterate through your result you can do a variety of things, but the most 'C#/Linq' way of doing it is via foreach; note that if you skip the enumeration at the end of your method, and return an 'IEnumerable' (It is UserInfo, right?) you can avoid doing everything at the same time, and have the compiler do some optimizations.
In my brain, this code would look like this:
public IEnumerable<UserInfo> GetUserInfoFromUsernameAndPassword(string userName, string password)
{
return STE.tblUsers
.Where(x => x.UserName == userName && x.PWD == password)
.Select(x => new UserInfo
{
UserID = x.UserID,
UserRole = x.UserRole
});
}
public void Iterate()
{
foreach (var userInfo in GetUserInfoFromUsernameAndPassword("username", "Password"))
Console.WriteLine(userInfo); // Do other stuff here...
}
Since you're already 'using dynamic' in your example code, you might do something like this in the 'Select' instead, and skip the UserInfo class definition altogether:
public IEnumerable<dynamic> GetUserInfoFromUsernameAndPassword(string userName, string password)
{
return STE.tblUsers
.Where(x => x.UserName == userName && x.PWD == password)
.Select(x => new
{
x.UserID,
x.UserRole
});
}
Upvotes: 0