一千萬個為什麽

搜索

從VAR獲得價值

對於下面的代碼

var validate = from P in this.DataContext.Persons
                         where P.UserName.Equals(login) && P.Password.Equals(password)
                         select new
                         {
                             P.FirstName,
                             P.LastName,
                             P.EmailAddress
                         };

如果記錄存在,我想獲取名字並將其返回。我怎樣才能從var驗證獲得firstName?

最佳答案

validate here is going to be a set (IQueryable) of data. You may need to use FirstOrDefault(), for example:

var record = validate.FirstOrDefault();
if(record != null) {
    string firstName = record.FirstName;
}

轉載註明原文: 從VAR獲得價值