Reputation: 16117
I have query in link to SQL like this
var password = (from userAccounts in myDb.Physicians
where userAccounts.Phy_UserName == txtUserName.Text
select userAccounts).FirstOrDefault();
I want to select a specific column with that query, I want to retrieve the row value of userAccounts.Password
, userAccounts.Phy_FName
, userAccounts.Phy_Password
and so on.
is password.Phy_FName
allowed? assuming that we have a result? how do I select a value?
Upvotes: 1
Views: 9047
Reputation: 47367
You've got it right. your Phy_UserName
needs to be a column in your database. It's returning a Physicians
object and you can get each value as follows.
// `var physician` is the same as `Physician physician`
// where you're declaring a Physician object from your DBML file
// we're using `p` for `physician` in the lambda select function
var physician = (from p in myDb.Physicians
where p.Phy_UserName == txtUserName.Text
select p).FirstOrDefault();
// now that you've got a `physician` object, you can use the properties
// within the object as follows..
var password = physician.Password;
var firstName = physician.Phy_FName;
// etc
// please note that the physician object you have currently has ALL of the
// database fields for that particular physician
now if you choose to only select your needed properties, you will approach it a little differently.
// first you declare a poco to store only the needed properties.
public class PhysicianViewModel {
public string Password {get; set;}
public string FirstName {get; set;}
}
// then you run your select query and drill down to grab
// only the required data fields from the database
var physicianViewModel = (from p in myDb.Physicians
where userAccounts.Phy_UserName == txtUserName.Text
select new PhysicianViewModel {
Password = p.Password,
FirstName = p.Phy_FName }).FirstOrDefault();
// and using them is the same as the above example
// the only difference here is that the `physicianViewModel` doesn't
// contain ANY properties other than the ones you specified.
var password = physicianViewModel.Password;
var firstName = physicianViewModel.FirstName;
As per your comment below, this is a little aside pertaining to preventing a null
value being inserted into a string
(nvarchar
) field in the database.
The easiest way to do this is to add the required attribute to the property and ensure you're validating it before submitting it.
public class Physician {
[Required]
public string Password {get; set;}
public string Phy_FName {get; set;}
public string Phy_LName {get; set;}
}
If you set the allow null to false
in the database, and then update your DBML file to reflect the changes, then the [required]
attribute will be added to the property automatically.
Upvotes: 2
Reputation:
var password = (from userAccounts in myDb.Physicians
where userAccounts.Phy_UserName == txtUserName.Text
select new {
Password = physician.Password,
FullName = physician.Phy_FName }).FirstOrDefault();
Upvotes: 0
Reputation: 5596
var physician = (from p in myDb.Physicians
where p.Phy_UserName == txtUserName.Text
select p).FirstOrDefault();
var password = physician.Password;
var fName = physician.Phy_FName;
Upvotes: 0