如何将数据库 table 中的列放入动态列表中?
How to put a column from a database table into a dynamic list?
对于我的项目,我想访问数据库,尤其是一个 table,其中包含 BauTeilId 并将所有 BauTeilId 到控制器中的一个列表中
默认连接:
"DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=QualitätsKontrolleDB;Trusted_Connection=True;MultipleActiveResultSets=true"
控制器
字符串代码来自HTML,由6位数字
组成
private ApplicationDbContext Context = new ApplicationDbContext();
[HttpGet]
public IActionResult StartPage(string Code)
{
Debug.WriteLine(Code);
var list = Context.Result.All(c => c.BauTeilId);
return View();
}
Table 用于此
我只需要 BauTeilId
类型nID int
PruefungenID 整型
BildID 整型
BauTeilID 字符串
日期日期时间
xLabel 字符串
X整数
YLabel字符串
Y 整数
FehlerCode 字符串
名称字符串
FehlerGruppe1 字符串
FehlerGruppe2 字符串
结果整数
现在输出不存在,但应该是整个列。
如果只想从 table 获取 BauTeilId,例如 select BauTeilId from table
使用
(from t in Context.Result
select new {
t.BauTeilId
}).toList(); // .toList(); is not necessary, Linq returns an `IEnumerable `
那个 LINQ 语句,希望你明白了。
您可以像这样列出您想要的任何列:
var myDynamicList = Context.Result.Select(c=> new { c.BauTeilId }).ToList();
这将是一个动态类型的列表。例如,您可以这样调用第一个元素 BauTeilId :
myDynamicList.First().BauTeilId;
对于我的项目,我想访问数据库,尤其是一个 table,其中包含 BauTeilId 并将所有 BauTeilId 到控制器中的一个列表中
默认连接:
"DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=QualitätsKontrolleDB;Trusted_Connection=True;MultipleActiveResultSets=true"
控制器 字符串代码来自HTML,由6位数字
组成private ApplicationDbContext Context = new ApplicationDbContext();
[HttpGet]
public IActionResult StartPage(string Code)
{
Debug.WriteLine(Code);
var list = Context.Result.All(c => c.BauTeilId);
return View();
}
Table 用于此 我只需要 BauTeilId 类型nID int PruefungenID 整型 BildID 整型 BauTeilID 字符串 日期日期时间 xLabel 字符串 X整数 YLabel字符串 Y 整数 FehlerCode 字符串 名称字符串 FehlerGruppe1 字符串 FehlerGruppe2 字符串 结果整数
现在输出不存在,但应该是整个列。
如果只想从 table 获取 BauTeilId,例如 select BauTeilId from table
使用
(from t in Context.Result
select new {
t.BauTeilId
}).toList(); // .toList(); is not necessary, Linq returns an `IEnumerable `
那个 LINQ 语句,希望你明白了。
您可以像这样列出您想要的任何列:
var myDynamicList = Context.Result.Select(c=> new { c.BauTeilId }).ToList();
这将是一个动态类型的列表。例如,您可以这样调用第一个元素 BauTeilId :
myDynamicList.First().BauTeilId;