我有一張名為“Cowork”的桌子和另一張名為“Commune”的桌子。在控制器中,我收到一個名為 NameCommune 的引數,鑒于該值我需要找到與接收到的引數匹配的 ID,上述結果我需要評估該 ID 是否存在于“COWORK”表中。(這兩個表是相關的)我是使用 LINQ 的新手,對此有什么想法嗎?我嘗試了以下方法,但它回傳一個空的 [ ]
public IActionResult GetNearbyPlaces(string nameComuna)
{
IQueryable<Commune> queryCommune = _context.Commune;
IQueryable<Cowork> queryCowork = _context.Cowork;
var codeCommune = (from code in queryCommune where code.name == nameComuna select code.code);
var coworkList = (from c in _context.Cowork where c.commune_code == codeCommune.ToString() select c).ToList();
return Ok(coworkList); // This returns an empty [ ]
}
在我的公用表中,ID 或我的主鍵由名稱代碼表示。
uj5u.com熱心網友回復:
你可能想要這樣的東西:
public IActionResult GetNearbyPlaces(string nameComuna)
{
IQueryable<Commune> queryCommune = _context.Commune;
IQueryable<Cowork> queryCowork = _context.Cowork;
var query =
from code in queryCommune
where code.name == nameComuna
join c in _context.Cowork on code.code equals c.commune_code
select c;
return Ok(query.ToList());
}
或者可能:
public IActionResult GetNearbyPlaces(string nameComuna)
{
IQueryable<Commune> queryCommune = _context.Commune;
IQueryable<Cowork> queryCowork = _context.Cowork;
var query =
from c in _context.Cowork
join code in queryCommune.Where(x => x.name == nameComuna)
on c.commune_code equals code.code into codes
where codes.Any()
select c;
return Ok(query.ToList());
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/446621.html
