C# 在LINQ TO SQL中选择列的值

C# 在LINQ TO SQL中选择列的值,c#,sql,linq,linq-to-sql,C#,Sql,Linq,Linq To Sql,我在SQL的链接中有这样的查询 var password = (from userAccounts in myDb.Physicians where userAccounts.Phy_UserName == txtUserName.Text select userAccounts).FirstOrDefault(); 我想用该查询选择一个特定列,我想检索userAccounts.Pa

我在SQL的链接中有这样的查询

var password = (from userAccounts in myDb.Physicians
                            where userAccounts.Phy_UserName == txtUserName.Text
                            select userAccounts).FirstOrDefault();
我想用该查询选择一个特定列,我想检索
userAccounts.Password
userAccounts.Phy\u FName
userAccounts.Phy\u Password
等的行值


是否允许
密码.Phy\u FName
?假设我们有结果?如何选择一个值

你说得对。您的
Phy\u用户名
必须是数据库中的一列。它返回一个
对象,您可以按如下方式获得每个值

// `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
现在,如果您选择只选择所需的属性,您将以稍微不同的方式处理它

// 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;

编辑 根据您下面的评论,这是关于防止将
null
值插入数据库中的
string
nvarchar
)字段的一点旁白

最简单的方法是向属性添加必需的属性,并确保在提交属性之前对其进行验证

public class Physician {
    [Required]
    public string Password {get; set;}
    public string Phy_FName {get; set;}
    public string Phy_LName {get; set;}
}

如果在数据库中将allow null设置为
false
,然后更新DBML文件以反映更改,则
[必需]
属性将自动添加到属性中。

作为对@allentranks所做操作的解释,他只从数据库中选择了
密码
物理名称
。。。这样就可以不查询整行,而只查询所需的数据。您应该确保
var password
实际上指向某个POCO,以后可以使用该POCO。但是,如果firstName为null,将如何更改或阻止它添加null值?如果firstName属性是
字符串
,那么它应该不会成为系统的问题。
String
是一种引用类型,并且已经可以为Null。如果您想确保输入永远不会为Null,那么您需要确保您的数据库不允许
Null
,然后更新您的dbml文件。通过这样做,将有一个
[required]
属性添加到属性中,以防止其提交
null
值。然而,在这个特定的问题中,您希望从数据库中提取数据,所以这是一个不同的主题。我已经更新了我的答案,为您提供了更多的细节。我希望有帮助。
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;
 var password = (from userAccounts in myDb.Physicians
                 where userAccounts.Phy_UserName == txtUserName.Text
                 select new { 
                          Password = physician.Password,
                          FullName = physician.Phy_FName  }).FirstOrDefault();