I want to return an anonymous data retreived from a query in Linq. I dont know which kind of List<> return because the data is "var" anonymous.
public List<?????> QueryXmlUserLogin()
{
var data = from item in XDocumentObj.Descendants("User_Data")
select new
{
user = item.Element("user").Value,
password = item.Element("password").Value,
};
data.ToList();
return ????
Two data types come to mind, a Tuple<string, string>
or a KeyValuePair<string, string>
depending on their intended usage:
public List<Tuple<string, string>> QueryXmlUserLogin()
{
var data = from item in XDocumentObj.Descendants("User_Data")
select Tuple.Create(item.Element("user").Value, item.Element("password").Value);
return data.ToList();
}
Using these however, can often hide the meaning of return types. Since the method is public, you may be better creating a new class for the job:
public class UserCredentials
{
public string Username {get; set; }
public string Password {get; set; }
}
public List<UserCredentials> QueryXmlUserLogin() ...
As an aside, (based on the variable names) storing passwords in plaintext should not be done. Apologies if I'm incorrectly interpreting the variables or your example is contrived, but if anyone stumbles across this post - a hash of the password (and unique salt) should be stored rather than the password in plaintext.