如何根据一列获取数据库中的整行
本文关键字:数据库 获取 一列 何根 | 更新日期: 2023-09-27 18:24:22
我在PostGres工作,我有一行,比如下面的一行:
CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados Borlo 2222 México 05021 Mexico
现在我必须得到包含城市的整行作为México
?
如果是基于行号,它是这样的(非常容易):
select * from Customers limit RowNumber-1, 1
我知道如何基于行数获取行,但我不知道如何基于列数据获取行(在我的情况下是墨西哥)?
注意请注意,我必须将每一列存储在类列表中,我的意思是:
List<Coustomer> cstmr = new List<Coustomer>() ;
//做一些类似的设计
foreach(var column in cstmr)
column.CustomerName=Ana Trujillo; // in Mexico's case
column.Country=Mexico;
您只需要
select * from Customers where City = 'México';
有关使用WHERE子句的详细信息,请阅读此链接。
如果我正确理解你的问题,你希望将行的值放在List<Coustomer>
的对象中,而不是
这可能会对你有用
private IList<Coustomer> MakeCustomers(DataTable dt)
{
IList<Coustomer> list = new List<Coustomer>();
foreach (DataRow row in dt.Rows)
list.Add(MakeCustomer(row));
return list;
}
private Customer MakeCustomer(DataRow row)
{
int customerId = int.Parse(row["CustomerId"].ToString());
string CustomerName = row["CustomerName"].ToString();
string ContactName = row["ContactName"].ToString();
string country = row["Country"].ToString();
//Any other fields which might be pulling from DB
return new Customer(customerId, company, city, country);
}