我想获得Patient
的Firstname
从数据库中,但什么我得到当我运行这段代码仅仅是一个LINQ表达式字符串,而不是.
public static string GetPatientName(int bedNumber) { var name = ""; using (var data = new HospitalDBDataContext()) { if (data.Connection.State == ConnectionState.Closed) data.Connection.Open(); name = data.Patients.Where(x => x.BedNumber.Equals(bedNumber)) .Select(x => x.Firstname).ToString(); if (data.Connection.State == ConnectionState.Open) data.Connection.Close(); data.Dispose(); } return name; }
使这个表达式返回我需要的实际值的方法是什么?
name = data.Patients.Where(x => x.BedNumber.Equals(bedNumber)) .Select(x => x.Firstname).ToString();
应该:
name = data.Patients.Where(x => x.BedNumber.Equals(bedNumber)) .Select(x => x.Firstname).FirstOrDefault();
你需要实际选择.First()
或.FirstOrDefault()
.现在,您将整个IQueryable
语句转换为字符串.