Suppose I have following table:
public class User
{
public User()
{
}
public User(string name, string pass)
{
addUser(name, pass);
}
public void addUser(string name, string pass)
{
//todo cryptography
this.login = name;
this.password = pass;
}
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
[Unique, MaxLength(20)]
public string login { get; set; }
[MaxLength(20)]
private string password { get; set; }
public string group { get; set; }
}
What is a best way to get user name from User table? To get all users depending on some name condition I use this example function for my test purpose.
public async void GetRow(string name) { var query = dbConnection.Table<User>().Where(x => x.login.Contains(name)); var result = await query.ToListAsync(); foreach (var item in result) { User u = item as User; MessageDialog msgbox = new MessageDialog(u.login); await msgbox.ShowAsync(); } }
Suppose I want to get only 1 record from Users table based on a given name, what would be best way to do that. I tried something like this:
from u in dbConection.Table<User> select u.Login where u.Login = name;
Edit:
ad 1. Throws exception
ad 2.This works only if record exists in table, in other case throws exception
var query = (from s in dbConnection.Table<User>() where s.login == name && s.password == password select s).FirstAsync();
User qr = query.Result;
I find solution. This is a simple validation function. Thank You for Your help.
try
{
var query = (from s in dbConnection.Table<User>() where s.login == name && s.password == password select s).FirstAsync();
User x = await query;
if (x != null) return true;
else return false;
}
catch (Exception)
{
return false;
}
var some_strings = new List<string> {"Szcz","epan"};
string first_string = some_strings.FirstOrDefault();
//first_string = "szcz";
some_strings = new List<string>();
first_string = some_strings.FirstOrDefault();
//first_string = null;
if (first_string != null)
// do your stuff here.
if you were dealing with Int
's, then the default would have been zero. IF it's a custom class, it'll be whatever your default for the class is.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.